例5-5、例5-6和例5-7
class A{
public int f(int x,int y){
return x+y;
}
}
class B extends A{
public int f(byte x,int y){
return x*y;
}
}
public class Example5_5{
public static void main(String args[]){
int z=0;
B b=new B();
z=b.f(10,10);
System.out.println(z);
z=b.f((byte)10,10);
System.out.println(z);
}
}
class A{
int x,y;
A(){
x=100;
y=200;
}
A(int x,int y){
this.x=x;
this.y=y;
}
}
class B extends A{
int z;
B(int x,int y){
super(x,y);
z=300;
}
B(){
super();
z=800;
}
public void f(){
System.out.printf("x=%d,y=%d,z=%d\n",x,y,z);
}
}
public class Example5_6{
public static void main(String args[]){
B b1=new B(10,20);
b1.f();
B b2=new B();
b2.f();
}
}
public class Bank{
int savedMoney;
int year;
double interest;
public double computerInterest(){
interest=year*0.035*savedMoney;
System.out.printf("%d元存在银行%d年的利息:%f元\n",savedMoney,year,interest);
return interest;
}
}
public class ConstructionBank extends Bank{
double year;
public double computerInterest(){
super.year=(int)year;
double remainNumber=year-(int)year;
int day=(int)(remainNumber*1000);
interest=super.computerInterest()+day*0.0001*savedMoney;
System.out.printf("%d元存在建设银行%d年零%d天的利息:%f元\n",savedMoney,super.year,day,interest);
return interest;
}
}
public class BankOfDalian extends Bank{
double year;
public double computerInterest(){
super.year=(int)year;
double remainNumber=year-(int)year;
int day=(int)(remainNumber*1000);
interest=super.computerInterest()+day*0.0002*savedMoney;
System.out.printf("%d元存在大连银行%d年零%d天的利息:%f元\n",savedMoney,super.year,day,interest);
return interest;
}
}
public class Example5_7{
public static void main(String args[]){
int amount=5000;
ConstructionBank bank1=new ConstructionBank();
bank1.savedMoney=amount;
bank1.year=5.216;
double interest1=bank1.computerInterest();
BankOfDalian bank2=new BankOfDalian();
bank2.savedMoney=amount;
bank2.year=5.216;
double interest2=bank2.computerInterest();
System.out.printf("两个银行利息相差%f元\n",interest2-interest1);
}
}