当前位置:网站首页>Access Characteristics of Constructor under Inheritance Relationship

Access Characteristics of Constructor under Inheritance Relationship

2022-08-09 22:07:00 Learning to program small dishes

Inheritance is one of the three major characteristics of object-oriented
What is inheritance? Simply put, when multiple classes have the same attributes or behaviors, extract these contents into a single class, then these classes do not need to define these properties and behaviors, just inherit that class.

What are the benefits of inheritance:

The appearance of inheritance improves the reusability of code.

Inherited Format:

public class subclass extends superclass{}
Access characteristics of member variables in inheritance: whoever is on the left of the equal sign is the one who is accessing, if not, look up.
Access characteristics of member methods in inheritance: who is new, who is accessing, if not, look up.

When a subclass inherits the parent class, it inherits all the methods and properties of the parent class.
The property or method declared as private in the parent class, after the subclass inherits it, cannot be called directly due to the influence of encapsulation, but it can still be obtained;

So a brief introduction to what inheritance is, then let's

public class zi extends fu{private double shengao;private String name = "Li Si";public zi() {super();}public zi(double shengao, String name) {super();// super(); access the constructor of the parent class and write it in the constructor of the subclass.this.shengao = shengao;this.name = name;}public void show() {super.show();//super. The member method of the parent class is written in the member method of the subclassSystem.out.println(this.name);System.out.println(super.getName());//super. The parent class variable method is written in the member method of the subclass}}class fu{private int age = 18;private String name = "Zhang San";private String sex = "male";public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public fu() {super();//If there is no super(parameter) written in the constructor of a class, a super() is given by default; it must be the first statement}public fu(int age, String name, String sex) {super();this.age = age;//This class member variablethis.name = name;this.sex = sex;}public void show() {System.out.println(this.name);System.out.println(this.sex);System.out.println(this.age);}}
public class Test {public static void main(String[] args) {zi zi = new zi();zi.show();}}

Zhang Sanmale18Li SiZhang San

Access three variables with the same name:
super: parent class variable
this: member variable of this class
Direct access: local variable

Three uses of the super keyword:
    super. Parent class member variable; written in the member method of the subclass
  super. The parent class member method; written in the member method of the subclass
super(); Access the constructor of the parent class and write it in the constructor of the subclass.


Note:
1. Cannot exist with this() at the same time
2. Must be the first statement
3. If there is no explicit write super in the constructor of a class(parameter); then a super() is given by default;
            If a super(parameter); is displayed, it will not be given.


Three uses of the this keyword:
this. member variables of this class;
this. member methods of this class ();
this (parameter); access the "other" of this class"Construction
Note:
1. Written in the constructor
2. It can only be the first statement and cannot exist at the same time as super()
3. The constructor cannot be recursive
When a subclass object is created, the constructor of the superclass must be called.

Characteristics of inheritance in java:

Single inheritance, a subclass can only have one parent class
A parent class can be inherited by multiple subclasses.
Multi-level inheritance is possible in java
If there is no explicit declaration of a class's parent class, this class inherits from the java.lang.Object class.
All java classes (except java.lang.Object class) directly or indirectly inherit from java.lang.Object class


Method Overriding:
In the subclass, the method inherited from the parent class can be transformed according to the needs, also known as
method reset and override.When the program is executed, the methods of the subclass will override the methods of the superclass.
1. The method overridden by the subclass must have the same method name and parameter list as the method overridden by the parent class.
2. The return value type of the method overridden by the subclass cannot be greater than that of the overridden method in the parent classThe return value type of the method
3. The access authority used by the method overridden by the subclass cannot be less than the access authority of the overridden method in the parent class
The subclass cannot override the private permission declared in the parent classMethod
4. The exception thrown by the subclass method cannot be greater than the exception of the overridden method of the superclass

The method with the same name and parameters in the subclass and the superclass must be declared as non-static (that is, overriding) at the same time, or declared as static (not overriding) at the same time.Because static methods belong to classes, subclasses cannot override methods of superclasses.

原网站

版权声明
本文为[Learning to program small dishes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091857586300.html