【面向对象小练习】继承

知识点

继承 基础点练习

  1. 定义类A 和类B 如下

class A {
    int a = 1;
    double d = 2.0;
​
    void show() {
        System.out.println("Class A: a=" + a + "\td=" + d);
    }
}
​
class B extends A {
    float a = 3.0f;
    String d = "Java program.";
​
    void show() {
        super.show();
        System.out.println("Class B: a=" + a + "\td=" + d);
    }
}
​

(1) 若在应用程序的main 方法中有以下语句:

A a=new A();

a.show();

则输出的结果如何?

Class A: a=1 d=2.0

(2) 若在应用程序的main 方法中定义类B 的对象b:

B b=new B();

b.show();

则输出的结果如何?

Class A: a=1 d=2.0 Class B: a=3.0 d=Java program.

int show(int a,int b){return 0;}

}

2.下面那些函数可以存在于Demo的子类中。

class Demo{
int show(int a,int b){return 0;}
}
​
​//不能改变为私有属性,重写数据类型要相同,静态才能重写为静态,
!A.public int show(int a,int b){return 0;}//?
​
B.private int show(int a,int b){return 0;}//?
​
C.private int show(int a,long b){return 0;}//?
​
D.public short show(int a,int b){return 0;}//?
​
E.static int show(int a,int b){return 0;}//?

3.

(1)定义一个ManKind类,包括
成员变量int sex和int salary;
方法void manOrWoman():根据sex的值显示“man”(sex==1)或者“woman”(sex==0);
方法void employeed():根据salary的值显示“no job”(salary==0)或者“ =job”(salary!=0)。
(2)定义类Kids继承ManKind,并包括
成员变量int yearsOld;
方法printAge()打印yearsOld的值。
(3)定义类KidsTest,在类的main方法中实例化Kids的对象someKid,用该对象访问
其父类的成员变量及方法。
​
public class Mankind{
    int sex;
    int salary;
//set/get方法
    public void setSalary(int salary) {
        this.salary = salary;
    }
​
    public int getSalary() {
        return salary;
    }
​
    public void setSex(int sex) {
        this.sex = sex;
    }
​
    public int getSex() {
        return sex;
    }
     //无参、有参构造
    public Mankind(){}
    public Mankind(int sex,int salary){
        this.salary=salary;
        this.sex=sex;
    }
    //manOrWoman()、 employeed()方法
    public void manOrWoman() {
      if(sex==1){
          System.out.println("man");
      }  else if(sex==0){
          System.out.println("woman");
      }
    }
    public void employeed(){
        if (salary==0){
            System.out.println("no job");
        }else if (salary!=0){
            System.out.println("job");
        }
    }
}
class Kids extends Mankind{
    int yearsOld;
    //无参、有参构造
    public Kids(){}
    public Kids(int yearsOld){
        this.yearsOld=yearsOld;
    }
//set/get方法
    public void setYearsOld(int yearsOld) {
        this.yearsOld = yearsOld;
    }
​
    public int getYearsOld() {
        return yearsOld;
    }
//printAge方法
    public void printAge(){
        System.out.println(yearsOld);
    }
}
//测试类
public class demo3 {
    public static void main(String[] args) {
        Kids someKid = new Kids();
        someKid.manOrWoman();
        someKid.employeed();
    }
}
​
​
​
​
​
​

4.???根据下图实现类。在CylinderTest类中创建Cylinder类的对象,Cylinder类继承Circle类,设置圆柱的底面半径和高,并输出圆柱的体积。

Circle (圆)
-radius :double
Circle(): 构造器,将radius属性初始化为1
+setRadius(double radius) : void
+getRadius(): double
+findArea():double 计算圆的面积
​
Cylinder (圆柱)
-length:double
Cylinder(): 构造器,将length属性初始化为1
+setLength(double length):void
+getLength():double
+findVolume() :double 计算圆柱体积
public class demo3 {
    public static void main(String[] args) {
        Cylinder cylinder = new Cylinder();
        cylinder.setRadius(3.0);
        cylinder.setLength(2.0);
        System.out.println("面积为"+cylinder.findArea());
        System.out.println("体积为"+cylinder.findVolume());
    }
}
​
​
​
//定义类
public class Circle{
    //属性
 double radius;
//无参、有参构造
    public Circle(){
        radius=1;
    }
    public Circle(double radius){
​
       this.radius=radius;
    }
​
    public void setRadius(double radius) {
        this.radius = radius;
    }
​
    public double getRadius() {
        return radius;
    }
    public double findArea(){
//        double area=Math.PI*(radius*radius);
        return Math.PI*(radius*radius) ;
    }
}
class Cylinder extends Circle{
   private double length;
    //无参、有参构造
    public Cylinder(){
        length=1;
    }
    public Cylinder(double length){
​
        this.length=length;
    }
​
    public void setLength(double length) {
        this.length = length;
    }
​
    public double getLength() {
        return length;
    }
    public double findVolume(){
​
        return findArea(area)*length;
    }
}
​
​

5.

//按要求实现下列问题:实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty和Staff。具体要求如下:
1)  Person类中的属性有:姓名name(String类型),地址address(String类型),
电话号码telphone(String类型)和电子邮件地址email(String类型);
2)  Employee类中的属性有:办公室office(String类型),工资wage(double
类型),受雇日期hiredate(String类型);
3)  Faculty类中的属性有:学位degree(String类型),级别level(String类型);
4)  Staff类中的属性有:职务称号duty(String类型)。
    
/*需求:   现有对象Faculty p1 =new Faculty()和Staff p2 =new Staff (),请分别为p1的属性赋值“本科”和Staff类的p2的duty赋值“程序员”,然后打印,(打印信息除了要求的需要打印外,其他属性(包括父类)任选2个赋值,最后一起打印)*/

​
public class demo3 {
    public static void main(String[] args) {
        Faculty p1 =new Faculty("本科");
        Staff p2 =new Staff ("程序员");
        p1.setName("小王");
        p2.setName("小明");
        p1.setTelephone("124738");
        p2.setTelephone("124738");
        System.out.println(p1.getName()+p1.getTelephone()+p1.degree);
         System.out.println(p2.getName()+p2.getTelephone()+p2.duty);
​
    }
}
​
public class Person{
    String name;
    String address;
    String telephone;
    String email;
​
    public void setName(String name) {
        this.name = name;
    }
​
    public String getName() {
        return name;
    }
​
    public void setAddress(String address) {
        this.address = address;
    }
​
    public String getAddress() {
        return address;
    }
​
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
​
    public String getTelephone() {
        return telephone;
    }
​
    public void setEmail(String email) {
        this.email = email;
    }
​
    public String getEmail() {
        return email;
    }
}
​
​
​
class Employee extends Person{
    String office;
    double wage;
    String hiredate;
​
    public void setOffice(String office) {
        this.office = office;
    }
​
    public String getOffice() {
        return office;
    }
​
    public void setWage(double wage) {
        this.wage = wage;
    }
​
    public double getWage() {
        return wage;
    }
​
    public void setHiredate(String hiredate) {
        this.hiredate = hiredate;
    }
​
    public String getHiredate() {
        return hiredate;
    }
}
class Faculty extends Employee{
    String degree ;
    String level;
​
    public void setDegree(String degree) {
        this.degree = degree;
    }
​
    public String getDegree() {
        return degree;
    }
​
    public void setLevel(String level) {
        this.level = level;
    }
​
    public String getLevel() {
        return level;
    }
​
    public Faculty(){}
public Faculty(String degree){
    this.degree=degree;
​
}
​
}
class Staff extends Employee {
    String duty;
public Staff(){}
public Staff(String duty){
    this.duty=duty;
}
​
    public void setDuty(String duty) {
        this.duty = duty;
    }
​
    public String getDuty() {
        return duty;
    }
}

实验 类的继承,super

1、写一个名为 Account 的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:

账号 id,余额 balance,年利率 annualInterestRate;包含的方法:访问器方法(getter 和

setter 方法),返回月利率的方法 getMonthlyInterest(),取款方法 withdraw(),存款方法

deposit()。

Account

private int id

private double balance

private double annualInterestRate

public Account (int id, double balance, double annualInterestRate )

public int getId()

public double getBalance()

public double getAnnualInterestRate()

public void setId( int id)

public void setBalance(double balance)

public void setAnnualInterestRate(double annualInterestRate)

public double getMonthlyInterest()

public void withdraw (double amount)

public void deposit (double amount)

写一个用户程序测试 Account 类。在用户程序中,创建一个账号为 1122、余额为 20000、

年利率 4.5%的 Account 对象。使用 withdraw 方法提款 30000 元,并打印余额。

再使用 withdraw 方法提款 2500 元,使用 deposit 方法存款 3000 元,然后打印余额和月利率。

提示:在提款方法 withdraw 中,需要判断用户余额是否能够满足提款数额的要求,如果不

能,应给出提示。

运行结果如图所示:

2、创建 Account 类的一个子类 CheckAccount 代表可透支的账户,该账户中定义一个属性

overdraft 代表可透支限额。在 CheckAccount 类中重写 withdraw 方法,其算法如下:

如果(取款金额<账户余额),

可直接取款

如果(取款金额>账户余额),

计算需要透支的额度

判断可透支额 overdraft 是否足够支付本次透支需要,如果可以

将账户余额修改为 0,冲减可透支金额

如果不可以

提示用户超过可透支额的限额

要求:写一个用户程序测试 CheckAccount 类。在用户程序中,创建一个账号为 1122、余

额为 20000、年利率 4.5%,可透支限额为 5000 元的 CheckAccount 对象。

使用 withdraw 方法提款 5000 元,并打印账户余额和可透支额。

再使用 withdraw 方法提款 18000 元,并打印账户余额和可透支额。

再使用 withdraw 方法提款 3000 元,并打印账户余额和可透支额。

提示:

(1) 子类 CheckAccount 的构造方法需要将从父类继承的 3 个属性和子类自己的属性全

部初始化。

(2) 父类Account的属性balance被设置为private,但在子类CheckAccount的withdraw

方法中需要修改它的值,因此应修改父类的 balance 属性,定义其为 protected。

运行结果如下图所示:

代码:

//测试类
public class AccountTest{
    public static void main(String[] args) {
        //在用户程序中创建Account对象。
        Account account = new Account();
        account.setId(1122);//创建一个账号为 1122
        account.setBalance(20000);//余额为 20000、
        account.setAnnualInterestRate(4.5);年利率 4.5%
        account.withdraw(30000);//使用 withdraw 方法提款 30000 元,并打印余额。
        account.withdraw(2500);//再使用 withdraw 方法提款 2500 元,
        account.deposit(3000);//使用 deposit 方法存款 3000 元
        account.show(); //打印余额和月利率。
        CheckAccount checkAccount = new CheckAccount();//创建CheckAccount可透支的账户duix
        checkAccount.setId(1122);//创建一个账号为 1122
        checkAccount.setBalance(20000);//余额为 20000
        checkAccount.setAnnualInterestRate(4.5);//年利率 4.5%
        checkAccount.setOverdraft(5000);//可透支限额为 5000 元
        checkAccount.withdraw(5000);
        checkAccount.withdraw(18000);
        checkAccount.withdraw(3000);
        checkAccount.show();
    }
}
public  class Account{
 private int id;
 protected double balance;
 private double annualInterestRate;
 //无参、有参构造方法
    public Account(){}
    public Account(int id , double balance,double annualInterestRate){
        this.annualInterestRate=annualInterestRate;
        this.balance=balance;
        this.id=id;
    }
    //访问器方法(getter 和setter 方法),

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

    public int getId() {
        return id;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getBalance() {
        return balance;
    }

    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }
    //返回月利率的方法 getMonthlyInterest(),。
    public double getMonthlyInterest(){
        return getAnnualInterestRate()/12/100;
    }
    //取款方法 withdraw(),
    public void withdraw (double amount){
        if (amount<getBalance()){
            System.out.println("取款成功");
            setBalance(balance-amount);
        }else if (amount>getBalance()){
            System.out.println("余额不足");
            System.out.println("您的账户余额为"+getBalance());
        }
    }
    //存款方法deposit()
    public void deposit (double amount){
        setBalance(balance+amount);
    }
    public void show(){
        System.out.println("您的账户余额为:"+getBalance());
        System.out.println("月利率为:"+getMonthlyInterest());
    }
}
//建 Account 类的一个子类 CheckAccount 代表可透支的账户
class CheckAccount extends Account{
    //定义一个属性
    //overdraft 代表可透支限额
    private double overdraft;
    //无参构造
    public CheckAccount(){}
    //CheckAccount 的构造方法需要将从父类继承的 3 个属性和子类自己的属性全部初始化。
    public CheckAccount(int id , double balance,double annualInterestRate,double overdraft){
        super(id, balance, annualInterestRate);
        this.overdraft=overdraft;
    }

    public void setOverdraft(double overdraft) {
        this.overdraft = overdraft;
    }

    public double getOverdraft() {
        return overdraft;
    }

    @Override
    public void withdraw(double amount) {
        if (amount<getBalance()){
            System.out.println("取款成功");
            super.setBalance(balance-amount);
            System.out.println("您的账户余额为:"+getBalance());
            System.out.println("您的可透支额为:"+overdraft);
        }else if (amount>getBalance()){
            if(amount>overdraft+getBalance()){
                System.out.println("超过可透支限额!");
            }else if(amount<overdraft+getBalance()){
               if(overdraft-(amount-getBalance())>=0){
               overdraft=overdraft-(amount-getBalance());
               }else {
                   overdraft=0;
               }
                super.setBalance(0);
                System.out.println("您的账户余额为:"+getBalance());
                System.out.println("您的可透支余额为:"+overdraft);
            }
        }
    }

    @Override
    public void show() {
        System.out.println("您的账户余额为:"+getBalance());
        System.out.println("您的可透支余额为:"+overdraft);
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值