当前位置:网站首页>继承关系下构造方法的访问特点
继承关系下构造方法的访问特点
2022-08-05 02:11:00 【la go go】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
注意:把this()访问本类其他成员方法也加进去
前言
学习javaSE的面向对象重要点:封装,继承,多态。但当我们真的学习起来,就会发现学习过程中就会有各种各样的小问题,可能刚开始的时候,一些小问题可以容易解决,当学习进入到面向对象的时候,就会出现各种各样的问题,有时候想很久都没有思路,但是请不要放弃,最好的方式是先让大脑休息一会,我在学习的过程中也遇到很多问题,当时绞尽脑汁可能还是百思不得其解,但当第二天再拾起这个问题,往往有恍然大悟之效,但是学习过程中最大的禁忌就是一放九得过且过,抛之脑后,随着时间的推移,困惑越来越多,这样的学习过眼不过心。最终无法在学习的道路上走太远。学习感悟到此就告一段落咯,下面是学习面向对象过程中的一些难点总结,以供参考。
提示:以下是本篇文章正文内容,下面案例可供参考
一、继承【简单总结就是共性抽取,提高代码的复用性】
继承是面向对象最显著的一个特性。继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性和行为,并能扩展新的能力。
二、使用步骤
1、如何实现继承
语法是:public class 子类 extends 父类{ }
注意:1.子类继承父类,就可以使用父类中非private修饰的成员信息。
2.权限修饰符protected,子类可以直接使用。
代码如下(示例):
package com.shang.demo;
public class Manage extends Employee {
protected int bonus;
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
public Manage() {
super();
}
public Manage(String name,int age,int salary,int bonus) {
this.name=name;
this.age=age;
this.salary=salary;
this.bonus=bonus;
}
public void work() {
System.out.println("努力学习,天天向上");
}
}
2.继承关系下构造方法的访问特点
1、在子类的构造放方法中,如果没有显示的写super()【super()调用父类的构造方法】,则默认赠送一个super().
2、如果显示的写了super(),则不赠送了。
3、super(参数),必须是第一个语句。 【有参数就是调用父类有参数构造方法】 在创建子类对象的时候,一定会调用父类的某个构造方法。
代码如下(示例):
创建一个父类
public class Fu {
public int a = 10;
//父类的method成员方法
public void method() {
System.out.println("Fu.method()");
}
//父类的无参构造方法
public Fu() {
System.out.println("Fu.Fu()");
}
//父类的有参构造
public Fu(int a) {
System.out.println("Fu.Fu(int a)");
}
}
创建一个子类
public class Zi extends Fu{
public int a = 20;
//创建子类成员方法method
public void method() {
System.out.println("Zi.method()");
}
//子类的无参构造方法
public Zi() {
//super();//调用父类的无参数的构造方法,输出为Fu.Fu(),这个super如果不写就会默认赠送调用父类的无参构造。
super(10);//调用父类的有参构造,输出为Fu.Fu(int a)
System.out.println("Zi.zi()");
}
}
测试类:
public class Test01 {
public static void main(String[] args) {
Zi zi = new Zi();
System.out.println(zi.a);
zi.method();
new Zi();
}
}
3.this关键字【注意事项】
- this.本类的成员变量
- this(参数):调用本类的其他构造方法。
- this.本类的成员方法
a.必须写在构造方法中
b.必须是第一个语句。【this和super不能同时存在】
c.写了this(参数),之后就不再赠送super();
d.构造方法不能递归。
代码如下(示例):
创建父类
public class Fu extends Object{
public Fu() {
super();
System.out.println("Fu.Fu()");
}
}
创建子类
public class Zi extends Fu{
public int a = 10;
public void test() {
int a = 20;
System.out.println(a);//20
System.out.println(this.a);//10
//test方法中调用本类的show方法
show();
this.show();
}
public void show() {
System.out.println("Zi.show()");
}
public Zi() {
this(10);//调用本类的有一个int参数的构造方法
System.out.println("Zi.Zi()");
}
public Zi(int n) {
//super();
//this();
System.out.println("Zi.Zi(int n)");
}
public void a() {
b();
}
public void b() {
a();
}
}
创建测试类
public class Test01 {
public static void main(String[] args) {
Zi zi = new Zi();
zi.test();
new Zi();
}
}
总结
以上就是对于继承关系下对访问构造方法的特点,以供参考。
边栏推荐
- oracle将restful接口封装到视图中
- How to simply implement the quantization and compression of the model based on the OpenVINO POT tool
- Dotnet 6 Why does the network request not follow the change of the system network proxy and dynamically switch the proxy?
- 没有对象的程序员如何过七夕
- 关于#sql shell#的问题,如何解决?
- 2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
- 如何逐步执行数据风险评估
- 迁移学习——Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
- C语言基础知识 -- 指针
- 用@Mapper查询oracle的分区情况报错
猜你喜欢
[Endnote] Word inserts a custom form of Endnote document format
sql语句多字段多个值如何进行排序
CPDA|运营人如何从负基础学会数据分析(SQL)
直播预告|30分钟快速入门!来看可信分布式AI链桨的架构设计
The use of pytorch: temperature prediction using neural networks
为什么他们选择和AI恋爱?
汇编语言之源程序
《.NET物联网从零开始》系列
MySQL学习
Exploding the circle of friends, Alibaba produced billion-level concurrent design quick notes are too fragrant
随机推荐
Greenplum数据库故障分析——版本升级后gpstart -a为何返回失败
leetcode-另一棵树的子树
C language basics -- pointers
AI+小核酸药物|Eleven完成2200万美元种子轮融资
【genius_platform软件平台开发】第七十六讲:vs预处理器定义的牛逼写法!!!!(其他组牛逼conding人员告知这么配置来取消宏定义)
Object.defineProperty实时监听数据变化并更新页面
【日常训练】1403. 非递增顺序的最小子序列
Understand the recommendation system in one article: Recall 06: Two-tower model - model structure, training method, the recall model is a late fusion feature, and the sorting model is an early fusion
多线程(2)
iNFTnews | 对体育行业和球迷来说,NFT可以带来什么?
使用SuperMap iDesktopX数据迁移工具迁移ArcGIS数据
没有对象的程序员如何过七夕
KingbaseES V8 GIS data migration solution (2. Introduction to the capabilities of Kingbase GIS)
Utilities SAP ERP和ORACLE ERP的区别是哪些?
2022杭电多校第一场
关于#sql shell#的问题,如何解决?
Three handshake and four wave in tcp
PHP Skills Assessment
Jincang database KingbaseES V8 GIS data migration solution (3. Data migration based on ArcGIS platform to KES)