第2课第4节_Java面向对象编程_多态性_P【学习笔记】
摘要:韦东山android视频学习笔记
面向对象程序的三大特性之继承性:
1、向上转换:只能定义被子类覆写的方法,不能调用在子类中定义的方法。
class Father {
private int money;
public int getMoney() {return money; }
public void setMoney(int money) {this.money = money; }
public void printInfo() {System.out.println("This is Father");}
}
class Son extends Father{
public void printInfo() {System.out.println("This is son");}
public void playGame(){System.out.println("This is son");}
}
class Daughter extends Father{
public void printInfo() {System.out.println("This is Father");}
}
public class Cnv {
public static void main (String args[]) {
Son son = new Son();
Daughter daughter = new Daughter();
Father f = son;
f.printInfo();
//f.playGame();
f = daughter;
f.printInfo();
}
}
编译运行:

2、JAVA向下转换的例子,在进行对象的向下转换前,必须首先发生对象的向上转换.否则会编译不过.
class Father {
private int money;
public int getMoney() {return money; }
public void setMoney(int money) {this.money = money; }
public void printInfo() {System.out.println("This is Father");}
}
class Son extends Father{
public void printInfo() {System.out.println("This is son");}
public void playGame(){System.out.println("This is son");}
}
class Daughter extends Father{
public void printInfo() {System.out.println("This is Father");}
}
public class Cnv2 {
public static void main (String args[]) {
Father f = new Son();
Son son = (Son)f;
}
}
3、看一下,下面的例子,假如有一千个类继承了father这个类,如果我们要打印他们的信息,那样我们岂不是要写1千个print函数,下面的第二个代码则通过向上转换这个技巧实现.
class Father {
private int money;
public int getMoney() {return money; }
public void setMoney(int money) {this.money = money; }
public void printInfo() {System.out.println("This is Father");}
}
class Son extends Father{
public void printInfo() {System.out.println("This is son");}
public void playGame(){System.out.println("This is son");}
}
class Daughter extends Father{
public void printInfo() {System.out.println("This is Dauhter");}
}
public class Cnv3 {
public static void main (String args[]) {
Father f = new Father();
Son s = new Son();
Daughter d = new Daughter();
print(f);
print(s);
print(d);
}
public static void print(Father f){
f.printInfo();
}
public static void print(Son s){
s.printInfo();
}
public static void print(Daughter d){
d.printInfo();
}
}
通过向上转换实现:
class Father {
private int money;
public int getMoney() {return money; }
public void setMoney(int money) {this.money = money; }
public void printInfo() {System.out.println("This is Father");}
}
class Son extends Father{
public void printInfo() {System.out.println("This is son");}
public void playGame(){System.out.println("This is son");}
}
class Daughter extends Father{
public void printInfo() {System.out.println("This is Dauhter");}
}
public class Cnv4 {
public static void main (String args[]) {
Father f = new Father();
Son s = new Son();
Daughter d = new Daughter();
print(f);
print(s);
print(d);
}
public static void print(Father f){
f.printInfo();
}
}
上述两份代码编译运行结果一样:

4、instanceof: 用来判断一个对象是不是某个类的实例
class Father {
private int money;
public int getMoney() {return money; }
public void setMoney(int money) {this.money = money; }
public void printInfo() {System.out.println("This is Father");}
public void drink(){System.out.println("drink");}
}
class Son extends Father{
public void printInfo() {System.out.println("This is son");}
public void playGame(){System.out.println("playGame");}
}
class Daughter extends Father{
public void printInfo() {System.out.println("This is Dauhter");}
public void dance(){System.out.println("dance");}
}
public class Cnv5{
public static void main (String args[]) {
Father f = new Father();
Son s = new Son();
Daughter d = new Daughter();
printAction(f);
printAction(s);
printAction(d);
}
public static void printAction(Father f){
if (f instanceof Son){
Son s = (Son)f;
s.playGame();
}else if (f instanceof Daughter){
Daughter d = (Daughter)f;
d.dance();
}else if(f instanceof Father){
f.drink();
}
}
}
编译运行结果:

相关代码存放在github,可以下载https://github.com/zzb2760715357/100ask

第2课第4节_Java面向对象编程_多态性_P【学习笔记】的更多相关文章
- 第2课第3节_Java面向对象编程_继承性_P【学习笔记】
摘要:韦东山android视频学习笔记 面向对象程序的三大特性之继承性:继承性的主要作用就是复用代码.继承性也有一定的限制,如图一 图一 1.我们在第2课第2节_Java面向对象编程_封装性_P 中 ...
- 第2课第7节_Java面向对象编程_内部类_P【学习笔记】
摘要:韦东山android视频学习笔记 1.什么是内部类:在类的内部定义一个类,内部类可以访问类的私有属性 class Outer{ ; class Inner{ public void print ...
- 第2课第5节_Java面向对象编程_异常_P【学习笔记】
摘要:韦东山android视频学习笔记 java的异常处理的原则如下: 1.我们先写一个没有对异常处理的程序,在进行除法运算的时候,除数是非零的话,运行时没有问题的,但是除数为零的时候,运行就会有问 ...
- 第2课第2节_Java面向对象编程_封装性_P【学习笔记】
摘要:韦东山android视频学习笔记 面向对象程序的三大特性之封装性:把属性和方法封装在一个整体,同时添加权限访问. 1.封装性的简单程序如下,看一下第19行,如果我们不对age变量进行权限的管控 ...
- 第2课第1节_Java面向对象编程_类的引入_P【学习笔记】
摘要:韦东山android视频学习笔记 1. 面向对象编程的引入,我们先写一个简单的程序输出张三,李四的名字.代码如下,假如,现在我们要在名字前面添加籍贯广东,那样岂不是每个printf语句都得修改添 ...
- 第2课第6节_Java面向对象编程_包和权限_P【学习笔记】
摘要:韦东山android视频学习笔记 1.使用package定义编译的时候存放的位置 package a.b.c.d; public class Package { public static v ...
- 类和对象:面向对象编程 - 零基础入门学习Python037
类和对象:面向对象编程 让编程改变世界 Change the world by program 经过上节课的热身,相信大家对类和对象已经有了初步的认识,但似乎还是懵懵懂懂:好像面向对象编程很厉害,但不 ...
- .net 4.0 面向对象编程漫谈基础篇读书笔记
话说笔者接触.net 已有些年头,做过的项目也有不少,有几百万的,也有几十万的,有C/S的,也有B/S的.感觉几年下来,用过的框架不少,但是.net的精髓一直没有掌握.就像学武之人懂得各种招式,但内功 ...
- Python 进阶_OOP 面向对象编程_组合与继承
#目录 前言 组合 派生 通过继承来覆盖重载方法 最常用的重载场景实例方法的重载 从标准类中派生类方法的重载 前言 我们定义一个类是希望能够把类当成模块来使用,并把类嵌入到我们的应用代码中,与其他的数 ...
随机推荐
- centos7 docker安装Jenkins BlueOcean
Jenkins是一款Java开发的跨平台持续集成和持续发布的开源项目,Jenkins已经作为各大公司进行CI/CD的首选工具.而BlueOcean是Jenkins推出的一个插件,其目的就是让程序员执行 ...
- go调度: 第一部分-OS调度(操作系统调度)
开场白 这个是三篇博客中的第一篇, 用来提供go调度背后的机制和语法. 这篇博客主要关注操作系统调度. 三篇博客的顺序是: 1) go调度: 第一部分 - 操作系统调度 2) go调度: 第二部分 - ...
- 改变默认的多选框 checkbox 样式~
效果图: HTML代码: <label for="Checkbox1" style="display:none;"></label> & ...
- 五、Linux_ping命令
ping命令用法为:“ping 参数 目标主机”.其中参数为零到多个,目标主机可以是IP或者域名. 1.每隔0.6秒ping一次,一共ping 5次: [root@aiezu.com ~]# ping ...
- HTML&CSS基础-html标签的实体
HTML&CSS基础-html标签的实体 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HTML源代码 <!DOCTYPE html> <html&g ...
- PAT甲级1008水题飘过
题目分析:上去下来到达的时间和数量 #include<iostream> using namespace std; ]; int main(){ int n; while(scanf(&q ...
- 【Pet HDU - 4707 】【利用并查集找深度】
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const i ...
- Strength(HDU6563+2018年吉林站+双指针瞎搞)
题目链接 传送门 题意 你有\(n\)只怪,每只怪的伤害为\(a_i\),对手有\(m\)只怪,每只怪的伤害为\(b_i\),对手的怪有普通状态和防守状态(普通状态:如果你用攻击力为\(a_i(a_i ...
- jpa之No property buyerOpenId found for type OrderMaster! Did you mean 'buyerOpenid'?
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...
- ElementUI——报错汇总
前言 elementUI的报错汇总 错误 please transfer a valid prop path to form item! vue.esm.js?c5de:628 [Vue warn]: ...