实训第四天
继承Animal
public class Animal {
private String name;
private String color;
public Animal() {
}
public Animal(String name, String color) {
this.name = name;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void show() {
System.out.println("我的名字叫" + getName() + ",我的毛色是" + getColor());
}
}
public class Dog extends Animal{
private int teethNumber;
public Dog() {
}
public Dog(String name, String color, int teethNumber) {
super(name, color);
this.teethNumber = teethNumber;
}
public int getTeethNumber() {
return teethNumber;
}
public void setTeethNumber(int teethNumber) {
this.teethNumber = teethNumber;
}
@Override
public void show() {
System.out.println("我叫" + getName() + ",我的颜色是" + getColor() + ",牙齿的数量是" + getTeethNumber());
}
public void speak() {
System.out.println("我要永远做hz的...");
}
}
public class DogTest {
public static void main(String[] args) {
Dog dog = new Dog("zy", "粉色", 28);
dog.show();
dog.speak();
}
}
多肽Shape
public class Shape {
private double x;
private double y;
public Shape() {
}
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public void show() {
System.out.println("[x = " + x + ",y = " + y + "]");
}
}
public class Rect extends Shape{
private double length;
private double width;
public Rect() {}
public Rect(double abscissa, double ordinate, double length, double width) {
super(abscissa, ordinate);
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public void show() {
System.out.println("[x = " + getX() + ",y = " + getY() + ",length = " + length + ",width = " + width + "]");
}
}
public class Circle extends Shape{
private double r;
public Circle() {}
public Circle (double abscissa, double ordinate, double r) {
super(abscissa, ordinate);
this.r = r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
@Override
public void show() {
System.out.println("[x = " + getX() + ",y = " + getY() + ",r = " + r + "]");
}
}
public class ShapeTest {
public static void printRect(Rect r) {
r.show();
}
public static void printCircle(Circle c) {
c.show();
}
//既能打印矩形又能打印圆形
public static void printShape(Shape s) {
s.show();
}
//static修饰的方法只能调用静态方法
public static void main(String[] args) {
Shape s = new Shape(3, 4);
s.show();
Rect r = new Rect(1, 2, 3, 4);
r.show();
Circle c = new Circle(1, 2, 4);
c.show();
printRect(new Rect(1, 2, 3, 4));
printCircle(new Circle(1, 2, 4));
//父类型 引用变量名 = new 子类类型();
Shape s1 = new Rect(1, 2, 3, 4);
s1.show();
Shape s2 = new Circle(1, 2, 4);
s2.show();
printShape(new Shape(1, 2));
printShape(new Rect(1, 2, 3, 4));
printShape(new Circle(1, 2, 4));
}
}
抽象、接口
public interface Runner {
//省略了 public static final 关键字
String Info = "信息";
public void running();
}
public interface Hunter extends Runner{
void hunt();
}
public class Chinese implements Hunter{
@Override
public void hunt() {
System.out.println("正在捕猎");
}
@Override
public void running() {
System.out.println("正在全力奔跑");
}
}
public class ChineseTest {
public static void main(String[] args) {
Runner runner = new Chinese();
runner.running();
Hunter hunter = new Chinese();
hunter.hunt();
}
}