当前位置:网站首页>继承和多态(上)
继承和多态(上)
2022-07-06 09:19:00 【犇犇犇犇犇犇】
继承和多态
目录:
文章目录
1.继承
1.1为什么需要继承
Java中使用类对现实世界中实体来进行描述,类经过实例化之后的产物对象,则可以用来表示现实中的实体,但是现实世界错综复杂,事物之间可能会存在一些关联,那在设计程序是就需要考虑。
比如:人和狗都是动物
1.2继承的概念
继承主要解决的问题:共性的抽取,代码的复用
1.3继承的语法
class 子类 extends 父类
- 子类,派生类
- 基类,超类,父类
- extends代表子类继承了父类
注意:
- 子类会将父类的成员变量或方法继承到子类中
- 子类继承父类后,必须有自己新的成员与父类不同,否则就没有意义了
1.4父类成员的访问
在继承体系中,子类将父类中的方法和字段继承下来了,那在子类中能否直接访问父类中继承下来的成员呢?->可以
1.4.1子类中访问父类的成员变量
子类和父类中不存在同名成员变量
子类和父类成员变量同名的情况下
- 成员变量访问遵循就近原则
- super.成员变量

1.4.2子类中访问父类的成员方法
- 子类和父类中不存在同名成员方法
- 子类和父类成员方法同名的情况下
- 方法的重载不是必须在一个类中
- 方法的重写
- 就近原则
1.5super关键字
- 只能在非静态的方法中使用
- 在子类方法中,访问父类的成员变量和方法
- super()
- 子类对象构造时,先要帮助父类的成员进行初始化
- 只能在调用它的子类构造方法的第一行
该关键字的主要作用就是在子类中访问父类的成员
1.6子类构造方法
注意:
- 若父类显式定义无参或者默认的构造方法,在子类构造方法第一行默认有隐含的super()调用,即调用基类构造方法
- 如果父类构造方法是带有参数的,此时编译器不会再给子类生成默认的构造方法,此时需要用户为子类显式定义构造方法,并在子类构造方法中选择合适的父类构造方法调用,否则编译失败。
- 在子类构造方法中,super()调用父类构造时,必须是子类构造函数中第一条语句。
- super()只能在子类构造方法中出现一次,并且不能和this同时出现
1.7super和this
【相同点】
- 都是关键字
- 只能在类的非静态方法中使用,来访问非静态的数据
- 都必须放在调用他们方法的第一行
【不同点】
- this是当前对象的引用,当前对象即调用实例方法的对象,super相当于是子类对象中从父类继承下来部分成员的引用
- 在非静态成员方法中,this用来访问本类的方法和属性,super用来访问父类继承下来的方法和属性
- this是非静态成员方法的一个隐藏参数,super不是隐藏的参数
- 在构造方法中:this()用于调用本类构造方法,super()用于调用父类构造方法,两种调用不能同时在构造方法中出现
- 构造方法中一定会存在super()的调用,用户没有写编译器也会增加,但是this()用户不写则没有
1.8再谈初始化
代码块
结论:由父及子,静态先行
所有代码
class Base{
public int a = 1;
public int b = 2;
public void methodA(){
System.out.println("Base::methodA()");
}
public void methodB(){
System.out.println("Base::methodB()");
}
}
class Derived extends Base {
public int a = 3;
public int d = 4;
public void methodA(int val){
System.out.println("Derived::methodA(int)"+val);
}
public void methodB(){
System.out.println("Derived::methodB()");
}
public void test(){
methodA();
methodA(10);
methodB();
super.methodB();
System.out.println(this.a);
System.out.println(this.b);
System.out.println("访问父类的a:"+super.a);
System.out.println(this.d);
}
}
public class TestDemo {
public static void main(String[] args) {
Derived derived = new Derived();
derived.test();
}
}
class Animal{
public String name;
public int age;
static {
System.out.println("Animal的静态代码块");
}
{
System.out.println("Animal的实例代码块");
}
public Animal(){
System.out.println("Animal不带参数的构造方法");
}
public Animal(String name, int age){
this.name = name;
this.age = age;
}
public void eat(){
System.out.println(name+"正在吃饭!");
}
}
class Dog extends Animal{
// public String name;
// public int age;
public float weight;
static {
System.out.println("Dog的静态代码块");
}
{
System.out.println("Dog的实例代码块");
}
public Dog(){
//super();//默认提供
System.out.println("Dog的构造方法");
}
public Dog(String name,int age,float weight){
//super()只能在第一行
super(name,age);//显示调用父类的函数,来初识化此时子类继承过来的父类的属性
this.weight = weight;
}
// public void eat(){
// System.out.println(name+"正在吃饭!");
// }
public void bark(){
System.out.println(name+"汪汪汪!");
}
}
class Cat extends Animal{
public Cat(String name, int age) {
super(name, age);
}
// public String name;
// public int age;
// public void eat(){
// System.out.println(name+"正在吃饭!");
// }
public void miaomiao(){
System.out.println(name+"正在喵喵喵");
}
}
public class Test {
public static void main(String[] args) {
Dog dog = new Dog();
}
public static void main1(String[] args) {
Dog dog = new Dog("hah",2,10.5f);
dog.name = "狗子";
dog.eat();
dog.bark();
}
}
各位看官老爷,看完觉得还不不错的话,点赞评论加关注哦

2.多态
边栏推荐
- 记录:初次cmd启动MySQL拒接访问之解决
- 阿里云微服务(三)Sentinel开源流控熔断降级组件
- [算法] 剑指offer2 golang 面试题2:二进制加法
- 记录:动态Web项目servlet访问数据库404错误之解决
- [algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
- Record: Navicat premium can't connect to MySQL for the first time
- Problems and solutions of robust estimation in rtklib single point location spp
- Employment of cashier [differential constraint]
- [GNSS data processing] Helmert variance component estimation analysis and code implementation
- [algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
猜你喜欢

Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
![[算法] 剑指offer2 golang 面试题2:二进制加法](/img/c2/6f6c3bd4d70252ba73addad6a3a9c1.png)
[算法] 剑指offer2 golang 面试题2:二进制加法

Code example of MATLAB reading GNSS observation value o file

闇の連鎖(LCA+树上差分)

Liste des boucles de l'interface graphique de défaillance
![[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once](/img/f7/23ffc81ec8e9161c15d863c1a67916.png)
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once

Several high-frequency JVM interview questions

服务未正常关闭导致端口被占用
![[algorithm] sword finger offer2 golang interview question 5: maximum product of word length](/img/e0/cea31070d6365eb57013cdead4a175.png)
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length

抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
随机推荐
Fundamentals of UD decomposition of KF UD decomposition [1]
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
Record: solution of 404 error of servlet accessing database in dynamic web project
面试必备:聊聊分布式锁的多种实现!
C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
Matlab读取GNSS 观测值o文件代码示例
How to improve the deletion speed of sequential class containers?
Itext 7 生成PDF总结
[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
服务未正常关闭导致端口被占用
[算法] 剑指offer2 golang 面试题10:和为k的子数组
闇の連鎖(LCA+树上差分)
[algorithm] sword finger offer2 golang interview question 1: integer division
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
Fairygui gain buff value change display
Record: the solution of MySQL denial of access when CMD starts for the first time
Excel导入,导出功能实现
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix