当前位置:网站首页>继承和多态(上)
继承和多态(上)
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.多态
边栏推荐
- 第一人称视角的角色移动
- 面渣逆袭:Redis连环五十二问,三万字+八十图详解。
- [算法] 剑指offer2 golang 面试题1:整数除法
- Containers and Devops: container based Devops delivery pipeline
- Fairygui character status Popup
- [算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
- [algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
- FairyGUI条子家族(滚动条,滑动条,进度条)
- Usage differences between isempty and isblank
- [algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
猜你喜欢
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
Detailed explanation of balanced binary tree is easy to understand
第一人称视角的角色移动
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
Wechat applet development experience
Code example of MATLAB reading GNSS observation value o file
10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
系统设计学习(一)Design Pastebin.com (or Bit.ly)
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
随机推荐
Rt-ppp test using rtknavi
[algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
[untitled]
MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
如何保障 MySQL 和 Redis 的数据一致性?
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
How do architects draw system architecture blueprints?
记录:newInstance()过时的代替方法
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
Music playback (toggle & playerprefs)
Halcon knowledge: gray_ Tophat transform and bottom cap transform
Usage differences between isempty and isblank
Record: the solution of MySQL denial of access when CMD starts for the first time
GNSS positioning accuracy index calculation
Role movement in the first person perspective
第一人称视角的角色移动
Basic DOS commands
Implementation of Excel import and export functions
Redis介绍与使用
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance