当前位置:网站首页>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
边栏推荐
- 面试必备:聊聊分布式锁的多种实现!
- Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
- Record: solution of 404 error of servlet accessing database in dynamic web project
- [GNSS] robust estimation (robust estimation) principle and program implementation
- Record: newinstance() obsolete replacement method
- 使用rtknavi进行RT-PPP测试
- [algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
- Branch and loop statements
- [算法] 剑指offer2 golang 面试题2:二进制加法
- [算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
猜你喜欢
Code example of MATLAB reading GNSS observation value o file
记录:初次cmd启动MySQL拒接访问之解决
How to ensure data consistency between MySQL and redis?
平衡二叉树详解 通俗易懂
堆排序【手写小根堆】
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
几道高频的JVM面试题
Novatel board oem617d configuration step record
Answer to "software testing" exercise: Chapter 1
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
随机推荐
The earth revolves around the sun
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
Dark chain lock (lca+ difference on tree)
RTKLIB: demo5 b34f.1 vs b33
雇佣收银员【差分约束】
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
Novatel board oem617d configuration step record
[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
Error: symbol not found
Rt-ppp test using rtknavi
面渣逆袭:Redis连环五十二问,三万字+八十图详解。
WSL common commands
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
How do architects draw system architecture blueprints?
记录:初次cmd启动MySQL拒接访问之解决
Record: newinstance() obsolete replacement method
编辑距离(多源BFS)
堆排序【手写小根堆】
Problems and solutions of robust estimation in rtklib single point location spp
Music playback (toggle & playerprefs)