【java】学习路线13-多态继承
/*多态
创建一个父类的对象,用子类去初始化
如果要使用子类的方法,则需要把对象强制转换为子类的(xx)xxx
*/
public class Learn01{
public static void main(String[] aa){
Person p1=new Student(18);
p1.changeName("Remoo");
System.out.println("p1's name:"+p1.getName());
System.out.println(((Student)p1).getScore());//18
((Student)p1).changeScore(666);
System.out.println(((Student)p1).getScore());//666
}
}
class Person{
private String name;
//Construction method
public Person(){}
public Person (String name){
this.name = name;
}
//getter&setter
public void changeName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
class Student extends Person{
private int score;
//Construction method
public Student(int score){
this.score = score;
}
//getter&setter
public void changeScore(int score){
this.score = score;
}
public int getScore(){
return this.score;
}
}
最新文章
- python 类变量和实例变量
- FMDB中 databaseWithPath 的使用问题
- 【openGL】四面体
- .NET NLog 详解(一)
- Yii源码阅读笔记(二十九)
- 【转】android适配各种分辨率的问题
- Stanford机器学习---第四讲. 神经网络的表示 Neural Networks representation
- max_allowed_packet
- D3.js 选择元素和绑定数据/使用数据
- PAT 06-3 单词长度
- RSA和DES------加密和解密类
- HDU 5744 Keep On Movin
- Memcached学习(三)
- C#之网络
- ReactNative Android之原生UI组件动态addView不显示问题解决
- Gethub readme 撰写
- mysql创建表及插入数据操作步骤及注意要点
- java学习——异常处理机制
- 2018.11.01 NOIP训练 梭哈(模拟)
- 【Redis】1、Jedis对管道、事务以及Watch的操作来应对高并发