标签:
1.Person
package com.yfs.javase; public class Person { private String name; private int age; private char sex; private void privateMehtod() { System.out.println("call privateMehtod()..."); } public Person() { //System.out.println("创建Perosn对象..."); } public Person(String name) { this.name = name; } public Person(String name, int age, char sex) { this.name = name; this.age = age; this.sex = sex; } public void introduce() { System.out.println("I am Person...."); } public String toString() { return "姓名:" + name + " 年龄 :" + age + " 性别:" + sex; } public void speak() { System.out.println(name + " 工作了吗?"); } public void sleep() { System.out.println(name + " 睡觉了吗?"); } public void eat() { System.out.println(name + " 吃了吗?"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } }
2.Student
package com.yfs.javase; //实现代码重用 单继承 public class Student extends Person {// 继承Person private int score; @Override public void introduce() { System.out.println("I am student..."); } // 覆盖 重写父类的方法 @Override public String toString() { return super.toString() + " 成绩:" + score; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public Student() { System.out.println("创建Student对象..."); } // 扩展方法 public void study() { System.out.println(getName() + " 在学习..."); } }
3.Teacher
package com.yfs.javase; public class Teacher extends Person { private String job; public String getJob() { return job; } public void setJob(String job) { this.job = job; } @Override public void introduce() { System.out.println("I am teacher..."); } }
4.test1
package com.yfs.javase; public class Demo1 { public static void main(String[] args) { Person p1 = new Person(); p1.setName("张三"); Student s1 = new Student(); s1.setName("李四"); Teacher t1 = new Teacher(); t1.setName("王五"); p1.speak(); s1.speak(); s1.study(); t1.introduce(); //s1 = t1;//类型不匹配 都是person子类 //p1.study(); 子类的方法属性不能访问 //里氏替换 Person p2 = s1;//父类的引用指向子类对象 p2 = new Student(); p2.setName("Tom"); p2.sleep(); //对象操作由声明类型决定 //p2.study();//计算机技术人员 //具体执行由对象决定 p2 中具体是什么对象 p2 = new Teacher(); //多态 方法覆盖 p2.introduce();//父类声明的方法 子类覆盖父类的方法 } }
5.test2
package com.yfs.javase; public class Demo2 { public static void main(String[] args) { Person p1 = new Person(); p1.setName("张三"); Student s1 = new Student(); s1.setName("李四"); Teacher t1 = new Teacher(); t1.setName("王五"); Person p2 = new Student(); p2.setName("Tom"); //变回去 强制转换 Student s2 = (Student)p2; s2.study(); //Teacher t2 = (Teacher)p2; //查看对象的类型 getClass() System.out.println(p2.getClass().getName()); System.out.println(t1.getClass().getName()); //s1 = p1; p1 = s1; System.out.println(p1); System.out.println(s1); s1.setAge(20); p1.setSex(‘男‘); System.out.println(p1); System.out.println(s1); //p1.study(); } }
标签:
原文地址:http://www.cnblogs.com/feilongblog/p/4675456.html