1. (继承、this 和super 关键字)有以下代码
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()" ) ;
}
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 ( ) ;
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 ( ) ;
}
}
public class TestSuperSub {
public static void main ( String args[ ] ) {
Sub s = new Sub ( ) ;
s. m1 ( ) ;
s. 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 {
}
在
A . public int method ( ) { return 0 ; }
B . void method ( ) { }
C . void method ( int n) { }
12. * (方法覆盖)有如下代码
class Super {
private void method ( ) { }
}
class Sub extends Super {
}
在
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.
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.
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.
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.
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 元。
SalariedEmployee :Employee 的子类,拿固定工资的员工。属性:月薪
HourlyEmployee :Employee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。属性:每小时的工资、每月工作的小时数
SalesEmployee :Employee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率
BasePlusSalesEmployee :SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上
销售提成部分。属性:底薪。
根据要求创建SalariedEmployee 、HourlyEmployees 、SaleEmployee 和BasePlusSalesEmployee 四个类的对象各一个,并计算某个月这四个对象的工资。
注意:要求把每个类都做成完全封装,不允许非私有化属性。
类图如下: