首页 > 代码库 > java中08的动手动脑
java中08的动手动脑
任务一:
ATM机模拟:
代码:
//赵子鸣,20153131,信1505-2班
package Shi;
import java.util.Scanner;
abstract class Atm{
public abstract void zhuan(String a,double b);
public abstract void setmima(String a);
public abstract double getyue();
public abstract String getmima();
public abstract void set(String a,String b,String c,String d,double e);
public abstract boolean cha(String a);
public abstract boolean panduan(String a);
public abstract void qukuan(int a);
public abstract void cunkuan(int a);
public abstract void qukuan(double a);
public abstract void cunkuan(double a);
}
class Account extends Atm{
String biaoshi;
String name;
String mima;
String date;
double yue;
Account(){}
public void zhuan(String a,double b){
}
public void setmima(String a){
mima=a;
}
public double getyue(){
return yue;
}
public String getmima(){
return mima;
}
public void set(String a,String b,String c,String d,double e){
biaoshi=a;
name=b;
mima=c;
date=d;
yue=e;
}
public boolean cha(String a){
if(biaoshi.equals(a))
return true;
else
return false;
}
public boolean panduan(String a){
if(mima.equals(a))
return true;
else
return false;
}
public void qukuan(int a){
yue=yue-a;
}
public void cunkuan(int a){
yue=yue+a;
}
public void qukuan(double a){
yue=yue-a;
}
public void cunkuan(double a){
yue=yue+a;
}
}
public class Shidd1{
public static void main(String[] args) {
Account a[]=new Account[10];
for(int i=0;i<10;i++)
a[i]=new Account();
a[0].set("ABCDE123456","张三","123456","2016/11/15",100);
a[1].set("ABCDE234561","李四","234561","2016/11/15",200);
a[2].set("ABCDE345612","王五","345612","2016/11/15",600);
a[3].set("ABCDE456123","赵六","456123","2016/11/15",800);
a[4].set("ABCDE561234","剑圣","561234","2016/11/15",1000);
Scanner shu1=new Scanner(System.in);
Scanner shu2=new Scanner(System.in);
System.out.println("输出账户标识符");
String a1=shu2.next();
int t=0;
for(int i=0;i<10;i++){
if(a[i].cha(a1))
{
t=i;
break;
}
else
System.out.println("没找到此用户");
}
System.out.println("输入密码");
String a2=shu2.next();
if(a[t].getmima().equals(a2)){
boolean w=true;int q;
while(w){
System.out.println("请选择:");
System.out.println("1存款");
System.out.println("2取款");
System.out.println("3转账汇款");
System.out.println("4修改密码");
System.out.println("5查询余额");
System.out.println("6退出");
q=shu1.nextInt();
switch(q)
{
case 1:
int a4=0;
System.out.println("选择金额");
System.out.println("1.100");
System.out.println("2.500");
System.out.println("3.1000");
System.out.println("4.1500");
System.out.println("5.2000");
System.out.println("6.5000");
int a5=shu2.nextInt();
if(a[t].getyue()<a5)
System.out.println("余额不足,失败");
else{
switch(a5)
{
case 1:a4=100;break;
case 2:a4=500;break;
case 3:a4=1000;break;
case 4:a4=1500;break;
case 5:a4=2000;break;
case 6:a4=5000;break;
}
a[t].cunkuan(a4);
System.out.println("存款成功");}
break;
case 2:
System.out.println("输入取款金额");
int a6=shu2.nextInt();
a[t].qukuan(a6);
System.out.println("取款成功");
break;
case 3:
System.out.println("输入转账对象卡号");
String a7=shu2.next();
int a8=0,t2=0;
for(int i=0;i<10;i++){
if(a[i].cha(a7))
{t2=i;
a8=1;
break;}
}
if(a8==1)
{System.out.println("输入金额");
double a9=shu2.nextDouble();
a[t].qukuan(a9);
a[t2].cunkuan(a9);
System.out.println("转账成功");}
else
System.out.println("没找到此用户");
break;
case 4:
System.out.println("输入修改后的密码:");
String a3=shu2.next();
a[t].setmima(a3);
System.out.println("修改成功");
break;
case 5:
System.out.println("余额为:"+a[t].getyue());
break;
case 6:
w=false;
System.out.println("已退出");
break;
}
}
}
else
System.out.println("该卡已被锁定,无法操作");
shu1.close();
shu2.close();
}
}
截图:
动手动脑问题:
代码:
package Shi1;
public class ParentChildTest {
public static void main(String[] args) {
Parent parent=new Parent();
parent.printValue();
Child child=new Child();
child.printValue();
parent=child;
parent.printValue();
parent.myValue++;
parent.printValue();
((Child)parent).myValue++;
parent.printValue();
}
}
class Parent{
public int myValue=http://www.mamicode.com/100;
public void printValue() {
System.out.println("Parent.printValue(),myValue="http://www.mamicode.com/+myValue);
}
}
class Child extends Parent{
public int myValue=http://www.mamicode.com/200;
public void printValue() {
System.out.println("Child.printValue(),myValue="http://www.mamicode.com/+myValue);
}
}
截图:
结论:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。如果子类与父类有相同的字段,则子类中的字段会代替或隐藏父类的字段,子类方法中访问的是子类中的字段(而不是父类中的字段)。如果子类方法确实想访问父类中被隐藏的同名字段,可以用super关键字来访问它。如果子类被当作父类使用,则通过子类访问的字段是父类的!
在 java 语言中,多态性体现在两个方面:由方法重载实现的静态多态性(编译时多态)和方法重写实现的动态多态性(运行时多态)。(1)编译时多态在编译阶段,具体调用哪个被重载的方法,编译器会根据参数的不同来静态确定调用相应的方法。(2)运行时多态由于子类继承了父类所有的属性(私有的除外),所以子类对象可以作为父类对象使用。程序中凡是使用父类对象的地方,都可以用子类对象来代替。一个对象可以通过引用子类的实例来调用子类的方法。重写方法的调用原则(为重点):java 运行时系统根据调用该方法的实例,来决定调用哪个方法。对子类的一个实例,如果子类重写了父类的方法,则运行时系统调用子类的方法;如果子类继承了父类的方法(未重写),则运行时系统调用父类的方法。
Zoo1:
代码:
package Shi1;
public class Zoo
{
public static void main(String args[])
{
Feeder f = new Feeder("小李");
// 饲养员小李喂养一只狮子
f.feedLion(new Lion());
// 饲养员小李喂养十只猴子
for (int i = 0; i < 10; i++)
{
f.feedMonkey(new Monkey());
}
// 饲养员小李喂养5只鸽子
for (int i = 0; i < 5; i++)
{
f.feedPigeon(new Pigeon());
}
}
}
class Feeder
{
public String name;
public Feeder(String name)
{
this.name = name;
}
public void feedLion(Lion l)
{
l.eat();
}
public void feedPigeon(Pigeon p)
{
p.eat();
}
public void feedMonkey(Monkey m)
{
m.eat();
}
}
class Lion
{
public void eat()
{
System.out.println("我不吃肉谁敢吃肉!");
}
}
class Monkey
{
public void eat()
{
System.out.println("我什么都吃,尤其喜欢香蕉。");
}
}
class Pigeon
{
public void eat()
{
System.out.println("我要减肥,所以每天只吃一点大米。");
}
}
截图:
Zoo2:
代码:
package Shi;
public class Zoo
{
public static void main(String args[])
{
Feeder f = new Feeder("小李");
//饲养员小李喂养一只狮子
f.feedAnimal(new Lion());
//饲养员小李喂养十只猴子
for (int i = 0; i < 10; i++)
{
f.feedAnimal(new Monkey());
}
//饲养员小李喂养5只鸽子
for (int i = 0; i < 5; i++)
{
f.feedAnimal(new Pigeon());
}
}
}
class Feeder
{
public String name;
Feeder(String name)
{
this.name = name;
}
public void feedAnimal(Animal an)
{
an.eat();
}
}
abstract class Animal
{
public abstract void eat();
}
class Lion extends Animal
{
public void eat()
{
System.out.println("我不吃肉谁敢吃肉!");
}
}
class Monkey extends Animal
{
public void eat()
{
System.out.println("我什么都吃,尤其喜欢香蕉。");
}
}
class Pigeon extends Animal
{
public void eat()
{
System.out.println("我要减肥,所以每天只吃一点大米。");
}
}
截图:
Zoo3:
代码:
//package zoo3;
public class Zoo {
public static void main(String args[]) {
Feeder f = new Feeder("小李");
Animal[] ans = new Animal[16];
//饲养员小李喂养一只狮子
ans[0] = new Lion();
//饲养员小李喂养十只猴子
for (int i = 0; i < 10; i++) {
ans[1 + i] = new Monkey();
}
//饲养员小李喂养5只鸽子
for (int i = 0; i < 5; i++) {
ans[11 + i] = new Pigeon();
}
f.feedAnimals(ans);
}
}
class Feeder {
public String name;
Feeder(String name) {
this.name = name;
}
public void feedAnimals(Animal[] ans) {
for (Animal an : ans) {
an.eat();
}
}
}
abstract class Animal {
public abstract void eat();
}
class Lion extends Animal {
public void eat() {
System.out.println("我不吃肉谁敢吃肉!");
}
}
class Monkey extends Animal {
public void eat() {
System.out.println("我什么都吃,尤其喜欢香蕉。");
}
}
class Pigeon extends Animal {
public void eat() {
System.out.println("我要减肥,所以每天只吃一点大米。");
}
}
截图:
Zoo4:
代码:
package S;
import java.util.Vector;
public class Zoo {
public static void main(String args[]) {
Feeder f = new Feeder("小李");
Vector<Animal> ans = new Vector<Animal>();
//饲养员小李喂养一只狮子
ans.add(new Lion());
//饲养员小李喂养十只猴子
for (int i = 0; i < 10; i++) {
ans.add(new Monkey());
}
//饲养员小李喂养5只鸽子
for (int i = 0; i < 5; i++) {
ans.add(new Pigeon());
}
f.feedAnimals(ans);
}
}
class Feeder {
public String name;
Feeder(String name) {
this.name = name;
}
public void feedAnimals(Vector<Animal> ans) {
for (Animal an : ans) {
an.eat();
}
}
}
abstract class Animal {
public abstract void eat();
}
class Lion extends Animal {
public void eat() {
System.out.println("我不吃肉谁敢吃肉!");
}
}
class Monkey extends Animal {
public void eat() {
System.out.println("我什么都吃,尤其喜欢香蕉。");
}
}
class Pigeon extends Animal {
public void eat() {
System.out.println("我要减肥,所以每天只吃一点大米。");
}
}
截图:
java中08的动手动脑