JAVA核心技术I---JAVA基础知识(多态)

一:类转型

类转型:子类可以转父类,父类不可以转子类(除非父类对象本身就是子类)

1.父类

public class Human {

    public void say() {
System.out.println("Hello World");
} public void eat() {
System.out.println("eat food");
}
}

2.子类

public class Men extends Human{
public void plough() {
System.out.println("Men is ploughing");
} public void eat() {
System.out.println("Men is eating");
}
}

3.实现转型

public class HumanTest {
public static void main(String args[]) {
Men m=new Men();
m.eat();
//子类转父类
Human hm=
m;
hm.eat();
hm.say();
//hm.plough();//The method plough() is undefined for the type Human
//父类转子类(前提该父类是子类转过去的)
Men m2=
(Men)hm;
m2.eat();
m2.plough();
}
}
Men is eating
Men is eating
Hello World
Men is eating
Men is ploughing

4.问题:编译时报错和运行时报错

class Pencil  {
public void write (String content){
System.out.println( "Write",+content);
}
} class RubberPencil extends Pencil{
public void write (String content){
System.out.println("Rubber Write"+content);
} public void erase (String content){
System.out.println( "Erase "+content);
}
}
执行下列代码的结果是哪项?
Pencil p=new Pencil();
(( RubberPencil) p).write("Hello");
C.运行时抛出异常

D.编译失败
强制转换是允许的,不过相当于会产生内存越界,导致运行时报错

JAVA核心技术I---JAVA基础知识(多态)

二:多态

多态:子类转型为父类后,调用普通方法,依旧是子类的

1.父类

public class Human {

    public void say() {
System.out.println("Hello World");
} public void eat() {
System.out.println("eat food");
}
}

2.子类:对父类方法重写

public class Women extends Human{
public void weave() {
System.out.println("women is weaving");
} public void say() {
System.out.println("Hello World,I am a women");
} public void eat() {
System.out.println("Women eat food");
}
}
public class Men extends Human{
public void plough() {
System.out.println("Men is ploughing");
} public void eat() {
System.out.println("Men is eating");
} public void say() {
System.out.println("Hello World,I am a man");
}
}
public class Child extends Human{
public void eat() {
System.out.println("Child is eating");
} public void say() {
System.out.println("Hello World,I am a Child");
}
}

3.多态实现

public class HumanTest {
public static void main(String args[]) {
Human[] HM= new Human[];
HM[]=new Men();
HM[]=new Women();
HM[]=new Child();
for(int i=;i<;i++) {
HM[i].eat();
HM[i].say();
}
}
}
Men is eating
Hello World,I am a man
Women eat food
Hello World,I am a women
Child is eating
Hello World,I am a Child

三:利用多态实现解耦

(一)父类实现解耦

public class HumanTest {
public static void main(String args[]) {
HumanSay(new Men());
HumanSay(new Women());
HumanSay(new Child());
HumanSay(new Child() {
public void say() {
System.out.println("Someone is eating");
}
});
} public static void HumanSay(Human a) {  //方法不是依赖某个具体子类,可以使用父类,抽象类,甚至接口来实现解耦
a.say();
} public static void HumanEat(Human a) {
a.eat();
}
}
Hello World,I am a man
Hello World,I am a women
Hello World,I am a Child
Someone is eating

(二)接口实现:

1.接口

public interface Human {
public void say();
public void eat();
}

2.子类

public class Men implements Human{
public void plough() {
System.out.println("Men is ploughing");
} public void eat() {
System.out.println("Men is eating");
} public void say() {
System.out.println("Hello World,I am a man");
}
}
public class Women implements Human{

public class Child implements Human{

3.其他一样,结果相同

(三)抽象类实现

1.抽象类

public abstract class Human {
public abstract void say();
public void eat() {
System.out.println("Human is eating");
}
}

2.子类

public class Men extends Human{
public class Women extends Human{
public class Child extends Human{

3.调用方式一样,结果一样

上一篇:Eclipse中Maven项目出现红色感叹号问题


下一篇:Eclipse新建Maven工程——git篇