Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the content related to inheritance in the foundation of Java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces what is the inheritance-related content in the Java foundation, which is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

What we share today is the foundation of Java-inheriting relevant content.

Method rewriting concept: a special case of a child parent class member method-override

When there is the same method as the parent class in the subclass, there will be an overwrite operation, also known as: override rewriting or overwriting.

Popular saying is that the subclass should use the function of the parent class, at the same time, it has its own unique function.

What to pay attention to in rewriting: the subclass method overrides the parent method and must ensure that the permission is greater than or equal to the permission of the parent class. There are four kinds of permissions, the largest is public, the smallest is private.

two。 Comparison of method overrides and method overloads:

Method rewriting: in the child parent class, the method declaration, method name, parameter list, and return value type must be the same.

Method overloading: the same class, method declaration, method name, different parameter list, regardless of the return value

3.this and super

When a subclass creates an object, the subclass object itself can be replaced by this, and the object space of the parent class in the object can be replaced by super.

Take person as the class, create a person's parent class, create a Chinese subclass, create a Test as a test, print that Xiaoming is 18 years old from the Institute of Science and Technology

# public class Person {

Private String name

Private int age

Public Person () {}

Public Person (String name,int age) {

This.name = name

This.age = age

}

}

Public class Chinese extends Person {

Private String address

Public Chinese () {}

Public Chinese (String name,int age,String address) {

Super (name,age)

This.address = address

}

}

Public class Test {

Chinese c = new Chinese (Xiaoming, 18, Chinese Academy of Science and Technology)

}

4.this and super call ordinary members and constructors

Call a normal member:

This. Member variables can access member variables of objects of this class

Super. Member variables can access member variables of parent objects

This. The member method () can access the member method of the object of this class

Super. The member method () can access the member method of the parent object

Call the constructor:

This (other parameters) can access other constructors of this class

Super (other parameters) can access other constructors of the parent class

Construction method:

Cannot inherit. Parent class no-parameter construction is called by default.

Super (): indicates access to the parent class no-parameter constructor

Super (parameter): indicates that the parent class has a parameter constructor

For example: use code to implement the following statement: use inherited methods instead of abstractions:

Wang Xiaoping, 30, with a salary of 8000 yuan, is eating.

Teacher Wang Xiaoping, 30, with a salary of 8000 yuan, is giving a lecture.

Xiao Wang, 14, with a score of 90.0, is eating a student meal.

Xiao Wang, a 14-year-old student with a score of 90.0, is studying

1. Define a Person class:

Public class Person {

Private String name

Private int age

Public Person () {}

Public Person (String name,int age) {

This.name = name

This.age = age

}

Public String getName () {

Return name

}

Public void setName (String name) {

This.name = name

}

Public int getAge () {

Return age

}

Public void setAge (int age) {

This.age = age

}

Public void eat () {

System.out.println (age+ "year old" + name+ "eating")

}

}

two。 Define a Teacher class:

Public class Teacher extends Person {

Private int salary

Public void eat () {

System.out.println ("salary is" + salary+ "yuan" + getAge () + "year-old" + getName () + "eating")

}

Public void lecture () {

System.out.println ("salary is" + salary+ "yuan" + getAge () + "age" + getName () + "teacher is lecturing")

}

Public Teacher () {}

Public Teacher (String name,int age,int salary) {

Super (name,age)

This.salary = salary

}

Public int getSalary () {

Return salary

}

Public void setSalary (int salary) {

This.salary = salary

}

}

3. Create a student class:

Public class Student extends Person {

Private double score

Public void eat () {

System.out.println ("score of" + score+ "+" getAge () + "year-old" + getName () + "eating student meal")

}

Public void study () {

System.out.println ("score of" + score+ "+" getAge () + "year-old" + getName () + "students are studying")

}

Public Student () {}

Public Student (String name, int age,int score) {

Super (name,age)

This.score = score

}

Public double getScore () {

Return score

}

Public void setScore (double score) {

This.score = score

}

}

4. Test:

Public class test01 {

Public static void main (String [] args) {

Teacher t = new Teacher ()

T.setName (Wang Xiaoping)

T.setAge (30)

T.setSalary (8000)

T.eat ()

T.lecture ()

Student s = new Student ("Xiao Wang", 14 ~ 90)

S.eat ()

S.study ()

}

}

On the basis of Java inheritance-related content is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report