Java day_06 重载、重写、多态、抽象类、接口

Cal

Cal

package day_06.Cal;

public class Cal {
    protected int num1;
    protected int num2;
    public Cal() {
        super();
    }
    public Cal(int num1, int num2) {
        super();
        this.num1 = num1;
        this.num2 = num2;
    }

    public int getResult() {
        System.out.println("Cal.getResult()");
        return 0;
    }
    public int getNum1() {
        return num1;
    }
    public void setNum1(int num1) {
        this.num1 = num1;
    }
    public int getNum2() {
        return num2;
    }
    public void setNum2(int num2) {
        this.num2 = num2;
    }
}

Add

package day_06.Cal;

public class Add extends Cal{
    public Add() {
        super();
    }
    public Add(int num1, int num2) {
        super(num1, num2);
    }
    @Override
    public int getResult() {
        return num1 + num2;
    }
}

CalMain

package day_06.Cal;

public class CalAddMainTest {
    public static void main(String[] args) {
        Add add=new Add(2,3);
        System.out.println(add.getResult());
    }
}

AbstractCal

Cal

package day_06.AbstractCal;

public abstract class Cal {
    protected int num1;
    protected int num2;

    public Cal() {
    }

    public Cal(int num1, int num2) {
        this.num1 = num1;
        this.num2 = num2;
    }

    public abstract int getResult();

    public int getNum1() {
        return num1;
    }

    public void setNum1(int num1) {
        this.num1 = num1;
    }

    public int getNum2() {
        return num2;
    }

    public void setNum2(int num2) {
        this.num2 = num2;
    }
}

Add

package day_06.AbstractCal;

public class Add extends Cal{
    public Add() {
    }

    public Add(int num1, int num2) {
        super(num1, num2);
    }

    @Override
    public int getResult() {
        return num1+num2;
    }
}

AbstractCalAddMain

package day_06.AbstractCal;

public class CalAddMainTest {
    public static void main(String[] args) {
        Add add=new Add(2,3);
        System.out.println(add.getResult());
    }
}

PerStud

Person

package day_06.PerStu;

public class Person {
    protected int id;
    protected String name;
    protected int age;

    public Person() {
    }

    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public void show(){
        System.out.println(name+"年龄是"+age+",id:"+id);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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;
    }
}

Student

package day_06.PerStu;

public class Student extends Person{
    private String className;

    public Student() {
    }

    public Student(int id, String name, int age, String className) {
        super(id, name, age);
        this.className=className;
    }
    @Override
    public void show(){
        super.show();
        System.out.println("在"+className+"班.");
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }
}

PerStudMain

package day_06.PerStu;

public class PerStuMainTest {
    public static void main(String[] args) {
        Student student=new Student(1,"zhang",20,"UI4");
        student.show();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值