当前位置:网站首页>Parent class reference to child class (parent class reference points to child class object)
Parent class reference to child class (parent class reference points to child class object)
2022-07-26 13:28:00 【A stream kite CD】
( I hope you can point out any mistakes , thank you .)
Use the premise : In the relationship between parent and child classes , The method to be called should be defined in the parent class .
for example :Animal a=new Dog();
Animal Class is Dog The parent of a class , The statement is a Animal class , In fact, it points to an object of a subclass That is to say new After the keyword .
keyword : polymorphic , Dynamic links , Upward transformation
1、 polymorphic
Method rewriting, overloading and dynamic linking constitute polymorphism , In the reference of parent class to child class, it is mainly reflected in the override of parent method by child class
2、 Dynamic links
When a method defined in the parent class is not overridden in the subclass , It's called the method of the parent class , If the subclass has a new method for the parent class , Then the method of the subclass is called .
3、 Upward transformation
In the example , In fact, it instantiates a subclass Dog Object of type ,Animal The parent of type a Just a quote from him
therefore Animal A reference to a type can point to Dog Type of object . This is it. “ Upward transformation ”
public class Animal {
public void animal1(){
}
public void animal2(){
}
}
public class Dog extends Animal {
public void animal1(){
System.out.println(" Inherited parent class And rewrite his method ");
}
public void animal1(String dog){
System.out.println(" Here is the right animal1() Method overloading .");
}
public static void main(String[] args) {
Animal a=new Dog();
a.animal2();// Here animal3() Method calls the method of the parent class
a.animal1();// This is because animal1() Method , So the reference of the parent type can call this method
a.animal1("sss");// The error here is because there is no definition in the parent class animal1(String dog) The method ,
// This method is only for animal1() An override of method , So a reference of a parent type cannot call this method
}
}
边栏推荐
- [flower carving hands-on] fun music visualization series small project (12) -- meter tube fast rhythm light
- Leetcode 2119. number reversed twice
- 时间复杂度和空间复杂度
- Exploration on cache design optimization of community like business
- Control the probability of random winning [C | random]
- JSON数据传递参数&日期型参数传递
- Team research and development from ants' foraging process (Reprint)
- B+树(3)聚簇索引,二级索引 --mysql从入门到精通(十五)
- Square root of leetcode 69. x
- [turn] judge the relationship between two geometries in ArcGIS
猜你喜欢

JSON数据传递参数&日期型参数传递

Algorithm -- continuous sequence (kotlin)

【Oauth2】五、OAuth2LoginAuthenticationFilter

A college archives management system based on asp.net

PostgreSQL official website download error

基于WebRTC和WebSocket实现的聊天系统

算法--连续数列(Kotlin)

Kubelet CRI container runtime

Unicode file parsing methods and existing problems

MVVM architecture encapsulation of kotlin series (kotlin+mvvm)
随机推荐
图扑 3D 可视化国风设计 | 科技与文化碰撞炫酷”火花“
Chat system based on webrtc and websocket
【开源之美】nanomsg(2) :req/rep 模式
7-25 0-1 backpack (50 points)
【Oauth2】五、OAuth2LoginAuthenticationFilter
key&key_len&ref&filtered(4)—mysql执行计划(五十)
B+树(3)聚簇索引,二级索引 --mysql从入门到精通(十五)
详解关系抽取模型 CasRel
File upload and download performance test based on the locust framework
Ultimate doll 2.0 | cloud native delivery package
从标注好的xml文件中截取坐标点(人脸框四个点坐标)人脸图像并保存在指定文件夹
Some practical operations of vector
Codeforces Round #810 (Div. 2)【比赛记录】
LeetCode 1523. 在区间范围内统计奇数数目
LeetCode 2119. 反转两次的数字
B+树(5)myISAM简介 --mysql从入门到精通(十七)
多线程使用不当导致的 OOM
AI-理论-知识图谱1-基础
8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
JSON data transfer parameters & date type parameter transfer