当前位置:网站首页>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 SanAccess 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.
边栏推荐
猜你喜欢

痛击面试官 CURD系统也能做出技术含量

DP-Differential Privacy概念介绍

prometheus学习3Grafana部署及基本使用

Can I make a TCP connection without accept?

Toronto Research Chemicals单羟基舒更葡糖钠说明书

39. 组合总和 && 40. 组合总和2 && 216. 组合总和3

小满nestjs(第五章 nestjs cli)

线性表的定义和基本操作

新起之秀 DPU,正在掀起数据中心变革!
![[] free column Android run Android, her - as command of safety](/img/d5/771802eb57f24c1cf88657f5c5a724.png)
[] free column Android run Android, her - as command of safety
随机推荐
访问控制知识
小满nestjs(第四章 前置知识装饰器-实现一个GET请求)
leetcode二叉搜索树与双向链表
面试官:Redis 大 key 要如何处理?
『百日百题 · 基础篇』备战面试,坚持刷题 第五话——循环语句(2)!
以技术创新加速国家“碳中和”建设进程,华为云创新中心助力欣冠精密实现云智控“气”
Prometheus Operator 通过additional 添加target
MySQL笔记-06 基础SQL操作
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰二氧化硅颗粒用
线性表的定义和基本操作
leetcode 二叉树的分层遍历1
PyTorch框架的 torch.cat()函数
IS31FL3737B general 12 x 12 LED drive 40 QFN I2C 42 ma
What are the benefits of enterprise data integration?How do different industries solve the problem of data access?
鲜花线上销售管理系统的设计与实现
MySQL, which is asked on both sides of the byte, almost didn't answer well
URL Protocol 网页打开应用程序
阿里二面:没有 accept,能建立 TCP 连接吗?
Oracle 字段自增
痛击面试官 CURD系统也能做出技术含量