day7继承

0,面向对象三大特性:继承,封装,多态
1,继承:xx是一种xx
public class Cat extends Animal
子类 父类,基类,超类
作用:简化代码(减少代码冗余)
构造方法:父类对象先与子类对象创建
super():调用父类构造函数
重写:当父类方法不能满足子类需求 方法名相同,参数也得相同,子类重写方法访问修饰符不能比父类更严格
equals hashcode
2,访问修饰符
public --公有 本类 同包 继承 无限制
protected --受保护的 本类 同包 继承
默认 本类 同包
private --私有的 本类
3,static – 静态的,全局唯一 Math

         静态方法中不能出现非静态元素

4,final 不可变的 – 类:不能有子类
属性:必须赋初值,若不赋初值,需要在构造函数中赋初值,一旦赋值, 值不能被改变
方法:不能被重写
局部变量:可以不赋初值。一旦赋值,值不能被改变

定义一个名为Vehicles(交通工具)的基类,该类中应包含String类型的成员属性brand(商标)和color(颜色),还应包含成员方法run(行驶,在控制台显示“我已经开动了”)和showInfo(显示信息,在控制台显示商标和颜色),并编写构造方法初始化其成员属性。
public class test {
public String brand;
public String color;
public void run (){
System.out.println(“我已经开动了”);
}

public void showInfo(){
	System.out.println(brand+color);
}
public void Vehicles (String brand,String color){
	this.brand = brand;
	this.color = color;
}

}

public class Vehicles {

/**
 * @param args
 */
public static void main(String[] args) {
	// TODO Auto-generated method stub
	test t = new test();
	t.run();
	t.brand=" 宝马";
	t.color = "红色";
	System.out.println(t.brand+t.color);
}

}

public class Truck extends test {
public int load;
public Truck(String brand, String color,int load){
super();
this.load = load;
}
public void showTruck(){
System.out.println(brand+" “+color+” "+load);
}
public Truck() {
super();}
}

编写Car(小汽车)类继承于Vehicles类,增加int型成员属性seats(座位),还应增加成员方法showCar(在控制台显示小汽车的信息),并编写构造方法。 编写Truck(卡车)类继承于Vehicles类,增加float型成员属性load(载重),还应增加成员方法showTruck(在控制台显示卡车的信息),并编写构造方法。 在main方法中测试以上各类。
public class Car extends Vehicles {

private int seats;

public Car (String brand, String color,int seats){
super();
this.seats = seats;
}
public Car() {
super();

}
public void showCar(){
System.out.println(brand+" “+color+” "+seats);
}

}

public class Test1{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Car c = new Car (“QQ”,“红色”,50);
c.showCar();
Truck t = new Truck (“东风”,“蓝色”,5);
t.showTruck();
}

}

ATM取款机

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。

运行结果如下图所示:

ATM:

public class Atm {

private String[][] count ={ {“1001”,“123456”,“500”},{“1002”,“123456”,“1500”}};
private String[] currentCount = null;

public boolean login (String name,String password){
boolean flag = false;
for (int i=0;i<count.length;i++){
if (count[i][0].equals(name)&&count[i][1].equals(password)){
flag=true;
currentCount = count[i] ;
break;
}
}
return flag;
}
public void getBalance(String name){
System.out.println(currentCount[2]);
}
public void saveMoney(int money){
currentCount[2] = Integer.parseInt(currentCount[2]) + money +" “;
}
public void getMoney(int money){
if( Integer.parseInt(currentCount[2])>=money){
currentCount[2] = Integer.parseInt(currentCount[2]) - money +” ";
}
else {
System.out.println(“余额不足”);
}

}
public void tranMoney(String other,int money){
getMoney( money);
for (int i = 0; i<count.length;i++){
if (count[i][0].equals(other)){
count[i][2] =Integer.parseInt(count[i][2])+money+"";
}
}

}
}

Test:

import java.util.Scanner;

public class Test {

private static String other;

/**
 * @param args
 */
public static void main(String[] args) {
	// TODO Auto-generated method stub

Atm atm = new Atm();
Scanner sc = new Scanner(System.in);
System.out.println(“username”);
String name = sc.next();
System.out.println(“password”);
String password = sc.next();
boolean b = atm.login(name, password);
while (b){
System.out.println(“1-余额 2-存款 3-取款 4-转账”);
int a = sc.nextInt();
switch (a){
case 1:
atm.getBalance(name);
break;
case 2:
System.out.println(“请输入存款金额”);
int money = sc.nextInt();
atm.saveMoney(money);
break;

case 3:
	System.out.println("请输入取款金额");
	int money0 = sc.nextInt();
	atm.getMoney(money0);
	break;
	
case 4:
	System.out.println("请输入对方账号");
	int money1 = sc.nextInt();
	atm.tranMoney(other, money1);
	break ;
default:
		System.out.println("请输入正确的数字");
}}}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值