当前位置:网站首页>Duplicate names in molecular class methods
Duplicate names in molecular class methods
2022-07-27 16:49:00 【.Null】
Summary of knowledge points :Java Duplicate names in molecular class methods
4. Duplicate names in molecular class methods
If there are many num, The parent class has a , Subclasses have two , How to print the one you want correctly num Well ?
4.1 Parent class
public class Fu { public int num = 10; }
4.2 Subclass
public class Zi extends Fu {
public int num = 20;
public void methodZi(){
int num = 30;
System.out.println(num); //30, local variable System.out.println(this.num); //20, Member variables of this class
System.out.println(super.num);//10, Member variables of the parent class
} }
4.3 Look at subclass methods methodZi() Can you correctly distinguish three num
public class Demo03Main {
public static void main(String[] args) {
Zi zi = new Zi();
zi.methodZi(); //30, 20, 10
} }
4.4 summary : To print what you want correctly num, You can do this
local variable , The one above num = 30, You can write directly
Member variables of this class , above num = 20, use this. Member variable name
Member variables of the parent class , above num = 10, use super. Member variable name
- The problem of duplicate names of member methods in inheritance
Suppose that both the subclass and the parent class have a method called method() , How do you know which one to use ?
5.1 Parent class
public class Fu {
public void method(){
System.out.println(“ Method execution of parent class with duplicate name ”);
} }
5.2 Subclass
public class Zi extends Fu {
public void method(){
System.out.println(“ Subclass duplicate name method execution ”);
} }
5.3 stay main Call in method() Method
public class Demo04Main {
public static void main(String[] args) {
Fu fu1 = new Fu();
Fu fu2 = new Zi(); // Construct through subclasses fu2
Zi zi = new Zi();
fu1.method(); // Method execution of parent class with duplicate name , The parent method is used
fu2.method(); // Subclass duplicate name method execution , Using subclass methods zi.method(); // Subclass duplicate name method execution , Using subclass methods
} }
Conclusion :
Who is the object created , Whose classes are used to construct objects , Give priority to who , If not, look up .
such as Fu fu2 = new Zi();,fu2 It is constructed with Subclasses , that fu2.method() Is to use the subclass method
Be careful ! Whether it's a member variable or a member method , If not, look up for the parent class , Never look down for subclasses .
边栏推荐
- Opencv (II) -- basic image processing
- 密码学系列之:PKI的证书格式表示X.509
- TP5 paging some small points
- Cvxpy - latest issue
- Rotate the whole model in ADAMS
- Interpretation of C basic syntax: summarize some commonly used but easily confused functions (i++ and ++i) in the program (bit field)
- Embedded interview
- 获取当前时间的前N天和前后天的数组列表循环遍历每一天
- Analysis of PHP keyword replacement classes (avoid repeated replacement, keep and restore the original links)
- solidwork装配体导入到Adams中出现多个Part重名和Part丢失的情况处理
猜你喜欢

Filament Creator材质编辑工具的实现

Simulation generate report
mvc和mvp和mvvm的区别

(二)动态卷积之Dynamic Convolution

Cvxpy - latest issue
![[paper reading] single- and cross modality near duplicate image pairsdetection via spatial transformer compare](/img/33/8af12d58f4afeb807ebf9a90a2ea47.png)
[paper reading] single- and cross modality near duplicate image pairsdetection via spatial transformer compare

Implementation of ByteDance service grid based on Hertz framework

Life game, universe 25 and striver

SolidWorks Simulation曲线图属性设定

Snowflake ID (go Implementation)
随机推荐
The difference between MVC and MVP and MVVM
什么是jsp?
Replication of complex linked list
Implementation of ByteDance service grid based on Hertz framework
Flowable process custom attribute
Opencv (III) -- image segmentation
插入word中的图片保持高dpi方法
Gurobi——GRBModel
Scala branch control (single branch, double branch, multi branch), return value of branch judgment
知网、万方数据库免费下载论文------比连接学校内网速度快数倍不止(有的学校万方数据库不支持下载)
The method of inserting degree in word
word中插入度的方法
File类字节输入、输出流
【论文阅读】A CNN-transformer hybrid approach for decoding visual neuralactivity into text
Google Chrome reversecaptcha ad blocking
If you don't want to step on those holes in SaaS, you must first understand the "SaaS architecture"
Array analog linked list
solidwork装配体导入到Adams中出现多个Part重名和Part丢失的情况处理
json数据解析
excel skill