当前位置:网站首页>Inheritance and polymorphism (I)
Inheritance and polymorphism (I)
2022-07-06 13:07:00 【犇犇犇犇犇犇犇】
Inheritance and polymorphism
Catalog :
List of articles
- Inheritance and polymorphism
- 1. Inherit :yum:
- 1.1 Why inheritance is needed :artificial_satellite:
- 1.2 Concept of inheritance :artificial_satellite:
- 1.3 The grammar of inheritance :artificial_satellite:
- 1.4 Access to parent class members :artificial_satellite:
- 1.5super keyword :artificial_satellite:
- 1.6 Subclass construction method :artificial_satellite:
- 1.7super and this:artificial_satellite:
- 1.8 Let's talk about initialization :artificial_satellite:
- All of the code :artificial_satellite:
- 2. polymorphic
1. Inherit
1.1 Why inheritance is needed
Java Class is used to describe entities in the real world , Class is the product object after instantiation , Can be used to represent real entities , But the real world is complex , There may be some correlation between things , Then in designing the program, we need to consider .
such as : People and dogs are animals
1.2 Concept of inheritance
Inheritance mainly solves the problem : Extraction of commonness , Code reuse
1.3 The grammar of inheritance
class Subclass extends Parent class
- Subclass , Derived class
- Base class , Superclass , Parent class
- extends Represents that the subclass inherits the parent class
Be careful :
- Subclasses will inherit the member variables or methods of the parent class into subclasses
- After the subclass inherits the parent class , You must have your own new member different from the parent class , Otherwise, it's meaningless
1.4 Access to parent class members
In the system of inheritance , The subclass inherits the methods and fields in the parent class , Can you directly access the members inherited from the parent class in the subclass ?-> Sure
1.4.1 A member variable in a subclass that accesses the parent class
There is no member variable with the same name in the subclass and parent class
When the subclass and parent member variables have the same name
- Member variable access follows the proximity principle
- super. Member variables
1.4.2 A member method in a subclass that accesses the parent class
- Member methods with the same name do not exist in subclasses and superclasses
- When the subclass and parent member methods have the same name
- Method overloading does not have to be in a class
- Method rewrite
- Nearby principle
1.5super keyword
- Can only be used in non static methods
- In subclass methods , Access the member variables and methods of the parent class
- super()
- When subclass objects are constructed , First, help the members of the parent class to initialize
- Only on the first line of the subclass constructor that calls it
The main function of this keyword is to access the members of the parent class in the subclass
1.6 Subclass construction method
Be careful :
- If the parent class explicitly defines No parameter or default Construction method of , Construct methods in subclasses first line The default is The implicit super() call , That is, call the base class constructor
- If the parent class constructor is with parameters , At this point, the compiler No default constructor will be generated for subclasses , At this point, you need to explicitly define the constructor for the subclass , And select Appropriate The parent class constructor calls , Otherwise the compilation fails .
- In the subclass constructor ,super() When calling the parent class construct , Must be in subclass constructor The first sentence .
- super() Can only occur once in a subclass constructor , And not with this At the same time
1.7super and this
【 The same thing 】
- It's all keywords
- Can only be used in non static methods of a class , To access non static data
- Must be placed on the first line of calling their methods
【 Difference 】
- this Is a reference to the current object , The current object is the object that calls the instance method ,super It is equivalent to the reference of some members inherited from the parent class in the subclass object
- In non static member methods ,this Used to access the methods and properties of this class ,super Used to access the methods and properties inherited from the parent class
- this Is a non static member method Hide parameters ,super Not a hidden parameter
- In the constructor :this() Used to call the constructor of this class ,super() Used to call the parent class constructor , Two calls cannot occur in a constructor at the same time
- There must be in the construction method super() Call to , If the user does not write the compiler, it will also increase , however this() If the user does not write, there is no
1.8 Let's talk about initialization
Code block
Conclusion : By father and son , Static first
All of the code
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(" Access to the parent class's 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 Static code block ");
}
{
System.out.println("Animal Example code block ");
}
public Animal(){
System.out.println("Animal The construction method without parameters ");
}
public Animal(String name, int age){
this.name = name;
this.age = age;
}
public void eat(){
System.out.println(name+" I am eating !");
}
}
class Dog extends Animal{
// public String name;
// public int age;
public float weight;
static {
System.out.println("Dog Static code block ");
}
{
System.out.println("Dog Example code block ");
}
public Dog(){
//super();// Provided by default
System.out.println("Dog Construction method of ");
}
public Dog(String name,int age,float weight){
//super() Only on the first line
super(name,age);// Show the function that calls the parent class , To first understand the properties of the parent class inherited by the subclass at this time
this.weight = weight;
}
// public void eat(){
// System.out.println(name+" I am eating !");
// }
public void bark(){
System.out.println(name+" Wang Wang Wang !");
}
}
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+" I am eating !");
// }
public void miaomiao(){
System.out.println(name+" Meow meow ");
}
}
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 ";
dog.eat();
dog.bark();
}
}
Look at the officers , If you don't think it's good after reading it , Like comments and pay attention
2. polymorphic
边栏推荐
- [algorithm] sword finger offer2 golang interview question 2: binary addition
- 继承和多态(上)
- Realization of the code for calculating the mean square error of GPS Height Fitting
- Tyut Taiyuan University of technology 2022 introduction to software engineering
- 2年经验总结,告诉你如何做好项目管理
- Several high-frequency JVM interview questions
- 阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
- GPS高程拟合抗差中误差的求取代码实现
- [algorithm] sword finger offer2 golang interview question 1: integer division
- [algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
猜你喜欢
Mixed use of fairygui button dynamics
MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
[算法] 剑指offer2 golang 面试题10:和为k的子数组
What are the advantages of using SQL in Excel VBA
Several high-frequency JVM interview questions
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
Iterable、Collection、List 的常见方法签名以及含义
What are the advantages of using SQL in Excel VBA
2022国赛Re1 baby_tree
Implementation of Excel import and export functions
随机推荐
Wechat applet development experience
Fairygui loop list
Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing
[Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
记录:动态Web项目servlet访问数据库404错误之解决
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
堆排序【手写小根堆】
[algorithm] sword finger offer2 golang interview question 1: integer division
121道分布式面试题和答案
Knowledge system of digital IT practitioners | software development methods -- agile
MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
Interview Essentials: talk about the various implementations of distributed locks!
[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K
图书管理系统小练习
阿里云一面:并发场景下的底层细节 - 伪共享问题
10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache