package Demo0125; public class Animal { private String name; private String color; public void show(){ System.out.println("我的名字叫做:" + getName() + ",我的颜色是:" + getColor()); } 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; } }
package Demo0125; public class Dog extends Animal { private int teethNumber; public void show(){ System.out.println("我叫" + getName() + ",颜色是" + getColor() + ",牙齿数量为" + getTeethNumber()); } public int getTeethNumber() { return teethNumber; } public void setTeethNumber(int teethNumber) { this.teethNumber = teethNumber; } public Dog() { } public Dog(int teethNumber) { this.teethNumber = teethNumber; } public Dog(String name, String color, int teethNumber) { super(name, color); this.teethNumber = teethNumber; } }
package Demo0125; public class DogTest { public static void main(String[] args) { Dog dog = new Dog(); dog.show(); Dog dog1 = new Dog("亚利", "粉色" ,1); dog1.show(); } }
package Demo0125.shape; public class Circle extends Shape{ private double radius; public Circle() { } public Circle(int x, int y, double radius) { super(x, y); this.radius = radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public Circle(double radius) { this.radius = radius; } @Override public void show(){ System.out.println("横坐标:" + getX() + " 纵坐标:" + getY() + " 半径:" + getRadius()); } }
package Demo0125.shape; public class Rect extends Shape{ private int length; private int width; public Rect() { } public Rect(int x, int y, int length, int width) { super(x, y); this.length = length; this.width = width; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public Rect(int length, int width) { this.length = length; this.width = width; } @Override public void show(){ System.out.println("横坐标:" + getX() + " 纵坐标:" + getY() + " 长度:" + getLength() + " 宽度:" + getWidth()); } }
package Demo0125.shape; public class Shape { private int x; private int y; public Shape(int x, int y) { this.x = x; this.y = y; } public Shape() { } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public void show(){ System.out.println("横坐标:" + getX() + " 纵坐标:" + getY()); } }
package Demo0125.shape; import Demo0125.shape.Circle; import Demo0125.shape.Rect; public class ShapeTest { public static void printRect(Rect rect){ rect.show(); } public static void printCircle(Circle circle){ circle.show(); } //既能打印Rect又能打印Circle public static void printShape(Shape shape){ shape.show(); } //static修饰的方法只能调用静态的方法 public static void main(String[] args) { Shape shape = new Shape(3,4); shape.show(); Rect rect = new Rect(1,2,3,4); rect.show(); Circle circle = new Circle(2,5,12); circle.show(); System.out.println("=============================="); Shape shape1 = new Rect(1,2,3,4); shape.show(); Shape shape2 = new Circle(1,2,3); shape2.show(); System.out.println("=============================="); printRect(new Rect(1,2,3,4)); printCircle(new Circle(3,4,5)); printShape(new Shape(1,2)); } }
package Demo0125.person; public class Person { private String name; private int age; public Person(){ } public Person(String name, int age) { this.name = name; this.age = age; } public void show(){ System.out.println("姓名:" + name + " 年龄:" + age); } }
package Demo0125.person; public class Worker extends Person{ private double salary; public Worker(String name, int age, double salary) { super(name, age); this.salary = salary; } @Override public void show() { super.show(); System.out.println("Salary: " + salary); } }
package Demo0125.person; public class PersonWorkerTest { public static void main(String[] args) { Person person = new Person("John", 30); System.out.println("Person:"); person.show(); System.out.println(); Worker worker = new Worker("Alice", 25, 5000.0); System.out.println("Worker:"); worker.show(); System.out.println(); Person p = new Worker("Bob", 35, 6000.0); System.out.println("Person -> Worker:"); p.show(); } }
package Demo0125.account; abstract class Account { protected double balance;//余额 public Account(double balance) { this.balance = balance; } public abstract double calculateInterest(); }
package Demo0125.account; import Demo0125.finaltest.A; class FixedAccount extends Account { public FixedAccount(double balance) { super(balance); } @Override public double calculateInterest() { return balance * 0.03; } }
package Demo0125.account; public class Test { public static void main(String[] args) { Account account = new FixedAccount(1000); System.out.println("利息为:" + account.calculateInterest()); } }
package Demo0125.run; public interface Runner { void running(); }
package Demo0125.run; public interface Hunter extends Runner{ void hunt(); }
package Demo0125.run; import org.junit.runner.Runner; public class Chinese implements Hunter { @Override public void hunt() { System.out.println("抓到一只小白兔..."); } @Override public void running() { System.out.println("正在全力奔跑..."); } }
package Demo0125.run; public class ChineseTest { public static void main(String[] args) { //接口类型的引用指向实现类的对象,形成多态 Runner runner = new Chinese(); runner.running(); Hunter hunter = new Chinese(); hunter.hunt(); } }