多态是面向对象的精髓,多态分静态和动态多态。
静态多态 ———— 重写重载
多态多态————编译期不知道多态的结果,要运行起来才会知道效果,根据绑定对象的不同而又不同结果。
instanceof做判断来判断传入参数
package yun.day.zuo;
public class Rectanble extends Shape{
private int width;
private int height;
public Rectanble(int height,int width){
this.height = height;
this.width = width;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
if (width > 0 && width < 15) {
this.width = width;
}
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
if (height > 0 && height < 15) {
this.height = height;
}
this.height = height;
}
void draw(){
for (int i = 0; i < this.width; i++) {
for (int j = 0; j < this.height; j++) {
if (i == 0 || i == width-1) {
System.out.print("*");
}
if (i > 0 && i < width-1) {
if (j == 0 || j == height - 1) {
System.out.print("*");
}else{
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
abstract:抽象类 可以在方法前面和类前面 在类前面叫抽象类 在方法前面叫抽象方法 抽象的东西只能被继承不能产生对象