java6面向对象三大特性练习题

1. (继承、thissuper 关键字)有以下代码
class Super{
    public Super(){
        System.out.println("Super()");
    }
    public Super(String str){
        System.out.println("Super(String)");
    }
}
class Sub extends Super{
    public Sub(){
        System.out.println("Sub()");//在这紧上面省略了Super();
    }
    public Sub(int i){
        this();
        System.out.println("Sub(int)");
    }
    public Sub(String str){
        super(str);
        System.out.println("Sub(String)");
    }
}
public class TestSuperSub{
    public static void main(String args[]){
        Sub s1 = new Sub();//调用Sub()无参构造
        System.out.println();//芝士换行,自己加的原题没有 
        Sub s2 = new Sub(10);
        System.out.println();
        Sub s3 = new Sub("hello");
    }
}
Super()
Sub()

Super()
Sub()
Sub(int)

Super(String)
Sub(String)
2.super)看下面代码,写出程序运行的结果
class Super{
    public void m1(){
        System.out.println("m1() in Super" );
    }
    public void m2(){
        System.out.println("m2() in Super" );
    }
}
class Sub extends Super{
    public void m1(){
        System.out.println("m1() in Sub");
        super.m1();//如果是仅有m1,则仍然调用本方法中的m1,会出现错误
    }
}
public class TestSuperSub{
    public static void main(String args[]){
        Sub s = new Sub();//创建子类数据类型的对象
        s.m1();//调用子类中的m1
        s.m2();//先从子类中找m2,没有就从父类中找
    }
}
//结果
m1() in Sub
m1() in Super
m2() in Super
8. (继承、对象构造过程)有以下代码
class ClassA{
	public ClassA(){
		System.out.println("ClassA()");
	}
}
class ClassB{
	public ClassB(){
		System.out.println("ClassB()");
	}
}
class ClassC extends ClassA{
	public ClassC(){
		System.out.println("ClassC()");
	}
}
class ClassD extends ClassB{
	private ClassA ca = new ClassA();//在成员变量的位置调用了无参构造
	private ClassC cc;
	public ClassD(){//构造方法就是为成员变量赋值,所以先执行上面的
		System.out.println("ClassD()");
	}
	public ClassD(int i){
		cc = new ClassC();
		System.out.println("ClassD(int)");
	}
}
public class TestConstructors{
	public static void main(String args[]){
		ClassD cd1 = new ClassD();
		System.out.println();
		ClassD cd2 = new ClassD(10);
	}
}
编译运行以上代码,请写出运行时输出的结果???????????????????????????????????BAD
ClassA()
ClassB()
ClassD()

ClassB()
ClassA()
ClassA()
ClassC()
ClassD(int)
9. *(继承、对象构造过程)有以下代码
class Meal{
	public Meal(){
		System.out.println("Meal()");
	}
}
class Lunch extends Meal{
	public Lunch(){
		System.out.println("Lunch()");
	}
}
class Vegetable {
	public Vegetable(){
		System.out.println("Vegetable()");
	}
}
class Potato extends Vegetable{
	public Potato(){
		System.out.println("Potato()");
	}
}
class Tomato extends Vegetable{
	public Tomato(){
		System.out.println("Tomato()");
	}
}
class Meat{
	public Meat(){
		System.out.println("Meat()");
	}
}
class Sandwich{
	Potato p = new Potato();
	Meat m = new Meat();
	Tomato t = new Tomato();
	public Sandwich(){
		System.out.println("Sandwich()");
	}
}
public class TestSandwich{
	public static void main(String args[]){
		Sandwich s = new Sandwich();
	}
}
写出这段代码的输出结果。
Vegetable()
Potato()
Meat()
Vegetable()
Tomato()
Sandwich()
10. *(默认构造函数)有以下代码
class Super{
}
class Sub extends Super{
	public Sub(){}
	public Sub(String str){
		super(str);
	}
}
问:该程序应该如何修改才能编译通过?
答:Super 类中添加一个无参构造和一个有 String 类型的有参数构造
 public Super(){}
 public Super(String srt){}
11. *(方法覆盖)有如下代码
class Super{
	int method(){return 0;}
}
class Sub extends Super{
	//1
}//1 处,能编译通过的代码为:AC
A. public int method(){return 0;}//访问权限大于父类可以
B. void method(){}//不可以,返回值是任意的,无法与父类区分
C. void method(int n){}//可以,有参数可以区分
12. *(方法覆盖)有如下代码
class Super{
	private void method(){}//父类的方法是私有的,子类无法继承
}
class Sub extends Super{//所以子类的方法写什么都行
	//1
}//1 处,能编译通过的代码为:ABCD
A. public int method(){return 0;}
B. void method(){}
C. void method(int n){}
D. private void method(){}
13. *(封装)已知一个类Student 代码如下:
class Student{
	String name;
	int age;
	String address;
	String zipCode;
	String mobile;
}
a)Student 的属性都作为私有,并提供get/set 方法以及适当的构造方法。
b)Student 类添加一个getPostAddress 方法,要求返回Student 对象的地址和邮编。
16. *(封装)已知一个类Student 代码如下:
class Student{
	String name;
	int age;
	String address;
	String zipCode;
	String mobile;
}
a)Student 的属性都作为私有,并提供get/set 方法以及适当的构造方法。
b)Student 类添加一个getPostAddress 方法,要求返回Student 对象的地址和邮编。
public class Test{//测试
	public static void main (string[] args){
		Student s = new Student("张三"23"家里蹲""10086","12421212512");
		System.out.println (s.getPostAddress());
	}
}
class Student{
    private String name;
    private int age;
    private String address;
    private String zipCode;
    private String mobile;
    public Student(String name, int age, String address, String zipCode, String mobile) {
        this.name = name;
        this.age = age;
        this.address = address;
        this.zipCode = zipCode;
        this.mobile = mobile;
    }
    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 String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getZipCode() {
        return zipCode;
    }
    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }
    public String getMobile() {
        return mobile;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
    public String getPsotAddress(){
        return address+" "+zipCode;
    }
}

18. *(封装、继承)设计如下的继承树:

在这里插入图片描述

18.Accout 表示银行账户,id 属性表示账户id,balance 表示账户余额,password表示账户密码;
SavingAccount 表示储蓄账户,interestRate 表示存款利率;
CreditAccount 表示信用账户,creditLine 表示信用额度。
完成下列任务:
1)所有属性都应设为私有,根据需要增加构造方法和get/set 方法。
2)修改setPassword 方法,要求:
setPassword 判断新密码长度是否是6位,如果不是则不予修改;
修改getPassword 方法,要求每次都返回 null 值。
3)修改interestRate 的set 方法,要求利率大于0 并小于10%
18.//Test
package BankAccount;
public class Test {
    public static void main(String[] args) {
        SavingAccount sa = new SavingAccount (125154455L ,20,"123456",10);
        System.out.println (sa.getPassword());
        sa.setInterestRate(0.04);
        System. out.println(sa.getInterestRate());
        sa.setPassword("12345678");
        System.out.println(sa.getPassword());
    }
}
18.//Account
package BankAccount;
public class Account{
    private Long id;
    private double balance;
    private String password;
    public Account() {
    }
    public Account(Long id, double balance, String password) {
        this.id = id;
        this.balance = balance;
        this.password = password;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public String getPassword() {
        return null;
    }
    public void setPassword(String password) {
        if(password.length()==6){
            this.password = password;
        }else{
            System.out.println("请输入六位数的密码");
        }
    }
}

18.//SavingAccount
package BankAccount;
public class Account{
    private Long id;
    private double balance;
    private String password;
    public Account() {
    }
    public Account(Long id, double balance, String password) {
        this.id = id;
        this.balance = balance;
        this.password = password;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public String getPassword() {
        return null;
    }
    public void setPassword(String password) {
        if(password.length()==6){
            this.password = password;
        }else{
            System.out.println("请输入六位数的密码");
        }
    }
}
18.//CreditAccount
package BankAccount;
public class CreditAccount extends Account {
    private double creditLine;
    public CreditAccount() {
    }
    public CreditAccount(Long id, double balance, String password, double creditLine) {
        super(id, balance, password);
        this.creditLine = creditLine;
    }
    public CreditAccount(double creditLine) {
        this.creditLine = creditLine;
    }
    public double getCreditLine() {
        return creditLine;
    }
    public void setCreditLine(double creditLine) {
        this.creditLine = creditLine;
    }
}
19. *(封装、继承)有以下几个类,根据下面的继承关系,用Java 代码实现。

在这里插入图片描述

a) Circle 类(圆形),属性:半径;方法:求周长、求面积
b) Rect 类(矩形),属性:长、宽;方法:求周长、求面积
c) Square 类(正方形),属性:边长;方法:求周长、求面积
提示:
1) 这三个类均具有求周长和面积的方法;
2) 正方形是特殊的矩形
24. **(封装、继承、super)某公司的雇员分为以下若干类:
Employee:这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
SalariedEmployeeEmployee 的子类,拿固定工资的员工。属性:月薪
HourlyEmployeeEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。属性:每小时的工资、每月工作的小时数
SalesEmployeeEmployee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率
BasePlusSalesEmployeeSalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上
销售提成部分。属性:底薪。
根据要求创建SalariedEmployeeHourlyEmployeesSaleEmployeeBasePlusSalesEmployee四个类的对象各一个,并计算某个月这四个对象的工资。
注意:要求把每个类都做成完全封装,不允许非私有化属性。
类图如下:

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值