黑马程序员技术交流社区
标题:
Polymorphism
[打印本页]
作者:
Ethan丶
时间:
2015-9-17 09:55
标题:
Polymorphism
本帖最后由 Ethan丶 于 2015-9-17 11:55 编辑
package com.itheima;
/*
* polymorphism:同一个对象,在不同时刻体现出来的状态
* cat-cat;dog-dog;animal-cat/dog
* 多态的特点:继承,方法重写,父类引用指向子类对象
*
*/
public class Polymorphism {
public static void main(String[] args) {
Animal c=new Cat(); //成员变量:编译看左边,运行看左边
Animal d=new Dog();
c.method();
System.out.println("----");
d.method();
// d.method1(); //成员方法:编译看左边,运行看右边
System.out.println(d.num);
// System.out.println(d.num2);
d.function(); //静态方法 编译看左边,运行看左边
}
/*//方法区
Dog d1 =new Dog();
Dog d2 =new Dog();
Dog d3 =new Dog();
methodKey(d1);
methodKey(d2);
methodKey(d3);
public static void methodKey(Dog d){
d.method();
d.method1();
}*/
}
class Cat extends Animal{
Cat (){
}
public void method(){
System.out.println("cat");
}
}
class Dog extends Animal{
public int num=1000;
public int num2=10;
Dog (){
}
public void method(){
System.out.println("dog");
}
public void method1(){
System.out.println("dog1");
}
public static void function(){
System.out.println("static dog");
}
}
class Animal {
int age;
public int num =100;
Animal(){
}
public static void function(){
System.out.println("static Animal");
}
public void method(){
System.out.println("Animal");
}
}
复制代码
作者:
Ethan丶
时间:
2015-9-17 11:55
package com.itheima;
/*
* polymorphism
* 多态向上向下转型
* 上:Far f=new Soon();
* 下:Soon s=(Son) f;
*/
public class PolyDemo {
public static void main(String[] args) {
Far f = new Soon();
f.teach();
System.out.println(f.age); //40
System.out.println("---");
Soon s = (Soon) f; //向下砖
s.teach();
s.play();
System.out.println(s.age); //20
}
}
//父类
class Far {
public int age = 40;
public void teach(){
System.out.println("教育");
}
}
//子类
class Soon extends Far {
public int age = 20; //子的age
public void teach(){
System.out.println("教育"); //重写父类
}
public void play(){
System.out.println("娱乐"); //子特有的
}
}
复制代码
作者:
Ethan丶
时间:
2015-9-17 11:56
Ethan丶 发表于 2015-9-17 11:55
polymorphism 的 总结
作者:
侯越强
时间:
2015-9-17 14:39
加油,越努力越幸运
作者:
Ethan丶
时间:
2015-9-17 21:47
自己的思路~
package com.itheima;
/*
* interface means like a
* abstract means is a
* Teacher and Student
* same:name,age,eat,sleep eat:man eat is abstract
* different: Teacher:smork
*/
public class Test7 {
public static void main(String[] args) {
Person tp = new Teacher();
Person sp = new Student();
tp.setAge(24);
tp.setName("teacher");
tp.sleep();
tp.eat();
System.out.print(tp.getAge());
System.out.println(tp.getName());
System.out.println("-------");
sp.setAge(12);
sp.setName("student");
sp.sleep();
tp.eat();
System.out.print(sp.getAge());
System.out.println(sp.getName());
System.out.println("-------");
PersonActivity pat = new TeacherSmo();
pat.smok();
System.out.println("-------");
PersonActivity pas = new StudentSmo();
pas.smok();
}
}
interface PersonActivity {
public abstract void smok();
}
class Person {
private String name;
private int age;
public Person(){}
public Person(String name,int age){
this.name = name;
this.age = age;
}
public void sleep(){
System.out.println("sleep");
}
public void eat(){}
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;
}
}
class Teacher extends Person {
public void eat(){
System.out.println("T's eat");
}
public Teacher(){}
public Teacher(String name,int age){
super(name,age);
}
public void sleep(){
System.out.println("T'sleep");
}
}
class Student extends Person {
public void eat(){}
public Student(){}
public Student(String name,int age){
super(name,age);
}
public void sleep(){
System.out.println("S'sleep");
}
}
class TeacherSmo implements PersonActivity {
public void eat(){}
public TeacherSmo(){}
public void smok(){
System.out.println("T'smok");
}
public void sleep(){
System.out.println("T'sleep");
}
}
class StudentSmo implements PersonActivity {
public void eat(){
System.out.println("S's eat");
}
public StudentSmo(){}
public void smok(){
System.out.println("S'smok");
}
public void sleep(){
System.out.println("s'sleep");
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2