/*多态
创建一个父类的对象,用子类去初始化
如果要使用子类的方法,则需要把对象强制转换为子类的(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;
    }

}

最新文章

  1. python 类变量和实例变量
  2. FMDB中 databaseWithPath 的使用问题
  3. 【openGL】四面体
  4. .NET NLog 详解(一)
  5. Yii源码阅读笔记(二十九)
  6. 【转】android适配各种分辨率的问题
  7. Stanford机器学习---第四讲. 神经网络的表示 Neural Networks representation
  8. max_allowed_packet
  9. D3.js 选择元素和绑定数据/使用数据
  10. PAT 06-3 单词长度
  11. RSA和DES------加密和解密类
  12. HDU 5744 Keep On Movin
  13. Memcached学习(三)
  14. C#之网络
  15. ReactNative Android之原生UI组件动态addView不显示问题解决
  16. Gethub readme 撰写
  17. mysql创建表及插入数据操作步骤及注意要点
  18. java学习——异常处理机制
  19. 2018.11.01 NOIP训练 梭哈(模拟)
  20. 【Redis】1、Jedis对管道、事务以及Watch的操作来应对高并发

热门文章

  1. CSCMS代码审计
  2. JS:this关键字1
  3. SAP FICO 常用table
  4. NC15975 小C的记事本
  5. 洛谷P2709 小B的询问 莫队做法
  6. Linux 启动流程及相关知识
  7. 丽泽普及2022交流赛day18 社论
  8. qbxt五一数学Day3
  9. 用js获取当前页面的url
  10. Vue 搭建脚手架 && 脚手架的文件结构 && 关于不同版本的Vue