1.25java

 

继承与多态

 Person

public class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person() {
    }
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public void show(){
        System.out.println("我的姓名是:" + name + ",我的年龄为:" + age);
    }
 Worker
public class Worker extends Person{
    private int money;
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
    public Worker() {
    }
    public Worker(String name, int age, int money) {
        super(name, age);
        this.money = money;
    }
    public void show() {
        System.out.println("我的姓名是:" + getName() + ",我的年龄为:" + getAge() + ",我的薪水有:" + getMoney());
    }
}
PersonWorkerTest
public class PersonWorkerTest {
    public static void main(String[] args) {
        Person person = new Person("niu",18);
        Person person1 = new Worker("niu",99,1);
        person.show();
        person1.show();
    }
}

Shape

public class Shape {
    private int x;
    private int y;
    public Shape() {
    }
    public Shape(int x, int y) {
        this.x = x;
        this.y = y;
    }
    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("");
    }

}
Rect
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;
    }
    @Override
    public void show(){
        System.out.println("矩形横坐标为:" + getX() + ",纵坐标为:" + getY() + ",长度为:" + getLength() + ",宽度为:" + getWidth());
    }
}
Circle
public class Circle extends Shape{
    private int r;
    public Circle() {
    }
    public Circle(int x, int y, int r) {
        super(x, y);
        this.r = r;
    }
    public int getR() {
        return r;
    }
    public void setR(int r) {
        this.r = r;
    }
    @Override
    public void show(){
        System.out.println("圆横坐标为:" + getX() + ",纵坐标为:" + getY() + ",半径为:" + r);
    }
}
 ShapeTest
public class ShapeTest {
    public static void printRect(Rect rect) {
        rect.show();
    }
    public static void printCircle(Circle circle) {
        circle.show();
    }
    //既能打印矩形又能打印圆形
    public static void printShape(Shape shape) {
        shape.show();
    }
    public static void main(String[] args) {
        Shape shape = new Shape(3,4);
        //多态1
        Shape shape1 = new Rect(3,4,6,5);
        Shape shape2 = new Circle(3,4,5);
        shape.show();
        shape1.show();
        shape2.show();

        printRect(new Rect(3, 4, 5, 6));
        printCircle(new Circle(1, 2, 3));
        //多态2
        printShape(new Rect(3, 4, 5, 6));
        printShape(new Circle(1, 2, 3));
        
    }
}

抽象与多态

Account

public abstract class Account {
    private double balance;
    public abstract double getMoney(double index, double time);
    public Account() {
    }
    public Account(double balance) {
        this.balance = balance;
    }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
}
FixedAccount

public  class FixedAccount extends Account{
    public FixedAccount() {
    }
    public FixedAccount(double balance) {
        super(balance);
    }
    @Override
    public double getMoney(double index, double time) {
        return getBalance() * index * time;
    }
}
AccountTest
public class AccountTest {
    public static void main(String[] args) {
        Account account = new FixedAccount(1000) ;
        System.out.println("本金:" + account.getBalance() + ",利息:" + account.getMoney(0.03,1));
    }

}

 接口

接口Hunter 

public interface Hunter extends Runner{
    void hunt();
}

接口Runner

public interface Runner{
    void running();
}

实现类Chinese

public class Chinese implements Hunter{

    @Override
    public void hunt() {
        System.out.println("抓到了");
    }

    @Override
    public void running() {
        System.out.println("跑的真快");
    }
    public static void main(String[] args) {
      Hunter hunter = new Chinese();
      hunter.hunt();
      Runner runner = new Chinese();
      runner.running();
    }
}

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值