Day8学习总结
程序员文章站
2024-02-24 15:09:54
...
复习:
面向对象的特性:继承 封装 多态 设计模式
xx是一种xx
封装-保证数据安全-属性私有化
多态-需要继承做基础,多种形态,重载
调用方法时,先找父类,没有会报错,有则会继续去子类中找,若有则执行子类方法,若无则执行父类方法
设计模式 -单例模式(懒汉式,饿汉式)
一个类最多只允许创建一个对象
1,构造函数私有
oracle – 神域
悲观性
==
equals
包装类
byte - Byte
short - Short
int - Integer
long - Long
float - Float
double - Double
char - Character
boolean- Boolean
instanceof:关键字 判断对象是否是该类实例
类之间的关系:继承 关联(聚合,组合):一个类作为另一个类属性存在
依赖:一个类作为另一个类方法的参数
静态块:static{} 类加载时被调用执行
非静态块:创建对象时
Integer Scanner
【练习题】1.多态练习
模拟天天酷跑游戏;
定义一个(宠物)Pet类,类中有属性name,方法是follow(跟随);再定义三个子类,Cat,Dog,Eagle,分别重写follow方法;
再定义一个Hero类,这个类中有两个属性name和Pet类型的pet,一个方法run(),再定义两个构造方法,Hero(String name,);Hero(String name,Pet pet);
run()方法的代码是Hero跑,并且他的宠物也跟着跑;
编写测试类来操作Hero
public class Pet {
private String name;
public void follow(){
System.out.println("zhe shi fu lei - animal");
}
}
public class Cat extends Pet {
public void follow(){
System.out.println("zhe shi zi lei - cat");
}
public void ccc(){
}
}
public class Dog extends Pet {
public void follow(){
System.out.println("zhe shi zi lei - dog");
}
public void aaa(){
}
}
public class Eagle extends Pet {
public void follow(){
System.out.println("zhe shi zi lei - eagle");
}
public void bbb(){
}
}
public class Hero {
private String name;
private Pet pet;
public Hero(String name) {
this.name = name;
}
public Hero(String name, Pet pet) {
this.name = name;
this.pet = pet;
}
public void run(){
System.out.println(this.name+"pao");
pet.follow();
}
}
测试类:
public class Test {
public static void main(String[] args){
Pet p = new Dog();
Hero h = new Hero("妲己",p);
h.run();
}
}
【练习题】2 编译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(){
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+"";
}
}
}
测试类:
import java.util.Scanner;
public class Test {
public static void main(String[] args){
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);
on:
while (b){
System.out.println("1-yuge 2-cunkuan 3-qukuan 4-zhuanzhang 5-tuichu");
int a = sc.nextInt();
switch (a){
case 1:
atm.getBalance();
break;
case 2:
System.out.println("请输入存款金额:");
int money = sc.nextInt();
atm.saveMoney(money);
break;
case 3:
System.out.println("请输入qu款金额:");
int money0 = sc.nextInt();
atm.getMoney(money0);
break;
case 4:
System.out.println("请输入对方账号:");
String other= sc.next();
System.out.println("请输入转账金额:");
int money1 = sc.nextInt();
atm.tranMoney(other,money1);
break;
case 5:
break on;
default:
System.out.println("请输入正确数字");
}
}
}
}