public class Shape {
void draw() {
System.out.println("draw Shape"); } void erase() {
System.out.println("erase Shape");
} public static void main(String[] args) {
// 向上转型的特性
Shape circle = new Circle();
circle.draw();
circle.erase();
}
}class Circle extends Shape {
void draw() {
System.out.println("draw Circle");
} void erase() {
System.out.println("earse Circle");
}
}输出:draw Circle
earse Circle为什么调用的是之类的方法?
相当于他的子类,把他父类里的方法覆盖了。
Shape circle = new Circle(); //这句很重要
也算多态。
调用的是Circle里的方法。
你自己定义的是子类的类型呀。
因为你new 的就是子类的.调用时先看自己有没有,有就调用自己的没有就调用父类的.
public class 人 {
void 虚虚() {
放水;
} public static void main(String[] args) {
人 ren = new 人();
ren.虚虚();
人 ren1 = new 男人();
ren.虚虚();
人 ren2 = new 女人();
ren.虚虚();
}
}class 男人 extends 人 {
void 虚虚() {
站着放水;
}
}
class 女人 extends 人 {
void 虚虚() {
蹲着放水;
}
}
这样得到的circle 是Shape类型的,但是是指向Circle(子类)的引用
调用的是子类中的方法,子类将父类的方法覆盖了,circle 是Shape类型的,但是指向Circle类的,所以调用子类中的方法。
声明的是shape,而实际呢创建的是Circle,在调用你创建的对象circle的时候
她搜索的方法范围在shape类当中,也就是说如果没有你所指定的方法就不会调用了,如果有则看是否重写了该方法,若是重写了则调用子类的方法,如果没有则调用继承的父类的方法。ok!
public class Shape {
void draw() {
System.out.println("draw Shape"); } void erase() {
System.out.println("erase Shape");
} public static void main(String[] args) {
// 向上转型的特性
Shape circle = new Circle();
circle.draw();//调用子类的方法
circle.erase();//调用子类的方法
circle.nomethod();//error
circle.fatherMethod();//调用继承的父类的方法
}
}class Circle extends Shape {
void draw() {
System.out.println("draw Circle");
} void erase() {
System.out.println("earse Circle");
}
}
void draw() {
System.out.println("draw Shape"); }
void fatherMetyhod(){
System.out.println("父类方法被调用");
}
void erase() {
System.out.println("erase Shape");
} public static void main(String[] args) {
// 向上转型的特性
Shape circle = new Circle();
circle.draw();//调用子类的方法
circle.erase();//调用子类的方法
circle.nomethod();//error
circle.fatherMethod();//调用继承的父类的方法
}
}class Circle extends Shape {
void draw() {
System.out.println("draw Circle");
} void erase() {
System.out.println("earse Circle");
}
}
上边的代码该一下,忘记添加fatherMethod方法
楼主Thinking in java 了吧?
那里说的不是很清楚吗
Shape circle = new Circle();//这叫父类引用指向子类,就可调用子类方法
public class Shape {
void draw() {
System.out.println("draw Shape"); } void erase() {
System.out.println("erase Shape");
} public static void main(String[] args) {
// 向上转型的特性
Shape circle = new Circle(); //这句话的意思是创建了一个Circle类的对象和声明了一个Shape引用,这个引用指向Circle对象,所以会调用Circle对象的方法
circle.draw(); //请记住这句话Shape circle;这种语句是声明引用,而不是对象, new出来的才是对象
circle.erase();
}
}class Circle extends Shape {
void draw() {
System.out.println("draw Circle");
} void erase() {
System.out.println("earse Circle");
}
}
你得明白在这种情况下,Shape circle = new Circle();
声明的是shape,而实际呢创建的是Circle,在调用你创建的对象circle的时候
她搜索的方法范围在shape类当中,也就是说如果没有你所指定的方法就不会调用了,如果有则看是否重写了该方法,若是重写了则调用子类的方法,如果没有则调用继承的父类的方法。ok! 正解
Inteface 形状{
public void 画图();
} class 圆形 implements 形状{
public void 画图(){System.out.prinltn("这是个圆形");}
} class 三角形implements 形状{
public void 画图(){System.out.prinltn("这是个三角形");}
} class Test {
public static void 画图(形状 graphic){
graphic.画图();
} public static void main(String []args){
形状 a = new 圆形();
形状 b = new 三角形();
Test.画图(a); //打印出 这是个圆形
Test.画图(b); //打印出 这是个三角形
}
} 以上代码就是多态的体现。。其中 接口 形状的画图方法 在test的画图方法中 展现出了 不同的行为 。
多态父类方法被子类重写,以父类观点来调用这个方法时,会去执行那个被子类重写后的方法
circle.draw();
circle.erase();当然是子类的了
所以调用的是子类里面的方法啊
看看你的代码class Circle extends Shape {
void draw() {
System.out.println("draw Circle");
} void erase() {
System.out.println("earse Circle");
}
}class Circle extends Shape 满足条件 继承(类)或实现(接口)
void draw()和void erase() 满足条件重写了父类的方法
Shape circle = new Circle();这里满足父类的引用指向子类的对象
为什么会调用子类的方法这样理解就可以了 circle 指向 Circle对象 尽管你声明circle 为Shape对象。
把子类当做父类来看待·· 多态嘛
这就要看你实际new出来的那个对象它是什么类型,父类引用可以指向子类对象,如下:
Shape circle = new Circle();
你实际new出来的是一个Circle类型的对象,所以它调用的就是Circle子类里定义的那个方法如果是这样:Shape circle = new Shape();
这时你new出来的就是一个Shape类型的对象,它调用的就是Shape父类的方法了