java 源代码 多态_java多态动手动脑

实验任务一:多态实现ATM工作原理

1)源代码:

package demo;

import java.util.Scanner;

class A{

String name;

String date;

String mima;

int yu;

String kahao;

public A(String name,String date,String mima,int yu,String kahao)

{

this.name=name;

this.date=date;

this.mima=mima;

this.yu=yu;

this.kahao=kahao;

}

public A(){}

public void way(){}

}

class Qukuan extends A{

public Qukuan(String str,String str1,String str2,int m,String str3){

name=str;

date=str1;

mima=str2;

yu=m;

kahao=str3;

}

public void way(){

System.out.println("100 200 1000 1500 2000 5000  1.其它金额   2. 退卡3.返回操作");

Scanner in=new Scanner(System.in);

int p=in.nextInt();

A a=new A(name,date,mima,yu,kahao);

if(p==100)

{

a.yu-=100;

System.out.println("取款成功,余额:"+a.yu);

}

else if(p==200)

{

a.yu-=200;

System.out.println("取款成功,余额:"+a.yu);

}

else if(p==500)

{

a.yu-=500;

System.out.println("取款成功,余额:"+a.yu);

}

else if(p==1000)

{

a.yu-=1000;

System.out.println("取款成功,余额:"+a.yu);

}

else if(p==1500)

{

a.yu-=1500;

System.out.println("取款成功,余额:"+a.yu);

}

else if(p==2000)

{

a.yu-=2000;

System.out.println("取款成功,余额:"+a.yu);

}

else if(p==5000)

{

a.yu-=5000;

System.out.println("取款成功,余额:"+a.yu);

}

else if(p==1)

{

System.out.println("请输入要取款的金额:");

int m=in.nextInt();

a.yu-=m;

System.out.println("取款成功,余额:"+a.yu);

}

else if(p==2){

return;

}

else if(p==3){

way();

}

}

}

class Cunkuan extends A{

public Cunkuan(String str,String str1,String str2,int m,String str3){

name=str;

date=str1;

mima=str2;

yu=m;

kahao=str3;

}

public void way(){

Scanner in=new Scanner(System.in);

System.out.println("请输入存款金额:");

int n=in.nextInt();

A a=new A(name,date,mima,yu,kahao);

a.yu+=n;

System.out.println("存款成功,余额:"+a.yu);

}

}

class Zhuanzhang extends A{

String kahao;

public Zhuanzhang(String str,String str1,String str2,int m,String str3){

name=str;

date=str1;

mima=str2;

yu=m;

kahao=str3;

}

public void way(){

A a=new A(name,date,mima,yu,kahao);

Scanner in=new Scanner(System.in);

System.out.println("请输入转账的卡号:");

String s=in.next();

A b=new A(name,date,mima,yu,s);

System.out.println("请输入转账的金额:");

int w=in.nextInt();

a.yu-=w;

b.yu+=w;

System.out.println("转账成功");

}

}

public class ATM {

public static void main(String args[]){

System.out.println("ATM系统\n选择语言:1 中文\t2 英文");

Scanner in=new Scanner(System.in);

int q=in.nextInt();

Qukuan a1=new Qukuan("zjx","2016.11.15","321165",6000,"45612354745");

Cunkuan a2=new Cunkuan("zjx","2016.11.15","321165",6000,"45612354745");

Zhuanzhang a3=new Zhuanzhang("zjx","2016.11.15","321165",6000,"45612354745");

if(q==1){

System.out.println("请输入密码:");

String str=in.next();

if(a1.mima.equals(str)){

System.out.println("1 取款\n2 存款\n3 转账\n4 退卡");

int t=in.nextInt();

if(t==1)

a1.way();

else if(t==2)

a2.way();

else if(t==3)

a3.way();

else

return;

}

else

System.out.println("密码错误");

}

else if(q==2)

{

System.out.println("Please input password:");

String str=in.next();

A a=new A("zjx","2016.11.15",str,6000,"654562346212");

if(a.mima==str){

System.out.println("1 qukuan\n2 cunkuan\n3 zhuanzhang\n4 tuika");

int t=in.nextInt();

if(t==1)

a1.way();

else if(t==2)

a2.way();

else if(t==3)

a3.way();

}

else

System.out.println("Password is not true.");

}

}

}

2)程序截图:

28c48278303d7bcb8e2b87111e11b434.png

d48856920c99d207f549864a002dee95.png

ad4491b80dc601eb8d1bdb089c1f07fe.png

45d3ed6a90ec0b0b9261602c00b4924a.png

实验任务二:课堂动手动脑

1)源代码:

package demo;

class Parent{

public int myValue=100;

public void printValue() {

System.out.println("Parent.printValue(),myValue="+myValue);

}

}

class Child extends Parent{

public int myValue=200;

public void printValue() {

System.out.println("Child.printValue(),myValue="+myValue);

}

}

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();

}

}

2)实验结果截图:

48796d8ca51c74061b4cf9ea852a96f8.png

3)程序分析:为何会得到这样的输出?

(1)父类分配空间,调用它的构造方法。

(2)子类分配空间,调用它自己的构造方法。

(3)子类赋值给父类,父类的子类有相同的方法,父类调用子类方法,并引用子类的对象,输出子类的值。

(4)父类中的变量值进行自加,但是只是父类中的值自加,调用的仍然是子类的方法,输出子类的值。

(5)将parent对象强制转化为Child,指向子类中的变量,调用子类方法并输出子类的值。

4)程序修改:

package demo;

class Parent{

public int myValue=100;

public void printValue() {

System.out.println("Parent.printValue(),myValue="+myValue);

}

}

class Child extends Parent{

public int myValue=200;

public void printValue() {

System.out.println("Child.printValue(),myValue="+myValue);

}

}

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();

((Child)parent).myValue++;

parent.printValue();

parent.myValue++;

parent.printValue();

}

}

截图:

523f8069b236632925a13ed7f345703b.png

分析:先将parent对象强制转化为Child,指向子类中的变量,输出子类的值。然后parent父类中的对象进行自加,对子类没有影响,调用子类方法,对象,输出子类的值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值