码迷,mamicode.com
首页 > 其他好文 > 详细

课后作业

时间:2016-11-18 22:53:43      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:编译错误   end   passwords   隐藏   osi   ring   tm机   return   考核   

课后作业

类型转换知识点考核-2

程序源代码:

 1 class Mammal{}
 2 class Dog extends Mammal {}
 3 class Cat extends Mammal{}
 4  
 5 public class TestCast
 6 {
 7      public static void main(String args[])
 8      {
 9             Mammal m;
10             Dog d=new Dog();
11             Cat c=new Cat();
12             m=d;
13             //d=m;
14             d=(Dog)m;
15             //d=c;
16             //c=(Cat)m;
17  
18      }
19 }

 

下列语句哪一个将引起编译错误?为什么?哪一个会引起运行时错误?为什么?

             m=d;

             d=m;

             d=(Dog)m;

             d=c;

             c=(Cat)m;

先进行自我判断,得出结论后,运行TestCast.java实例代码,看看你的判断是否正确。

答: d=m;和d=c;会引起错误。d = m错误的是没有对M强制类型转化。d=c错误是因为d 和c类型不同导致错误。

技术分享

实验二

程序源代码:

 1 public class ParentChildTest {
 2      public static void main(String[] args) {
 3             Parent parent=new Parent();
 4             parent.printValue();
 5             Child child=new Child();
 6             child.printValue();
 7             
 8             parent=child;
 9             parent.printValue();
10             
11             parent.myValue++;
12             parent.printValue();
13             
14             ((Child)parent).myValue++;
15             parent.printValue();
16             
17      }
18 }
19  
20 class Parent{
21      public int myValue=100;
22      public void printValue() {
23             System.out.println("Parent.printValue(),myValue="+myValue);
24      }
25 }
26 class Child extends Parent{
27      public int myValue=200;
28      public void printValue() {
29             System.out.println("Child.printValue(),myValue="+myValue);
30      }
31 }

 

回答问题:

1.    左边的程序运行结果是什么?

技术分享

2.    如何解释会得到这样的输出?

当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。

这个特性实际上就是面向对象“多态”特性的具体表现。

3.   计算机是不会出错的,之所以得到这样的运行结果也是有原因的,那么从这些运行结果中,你能总结出Java的哪些语法特性?

如果子类与父类有相同的字段,则子类中的字段会代替或隐藏父类的字段,子类方法中访问的是子类中的字段(而不是父类中的字段)。如果子类方法确实想访问父类中被隐藏的同名字段,可以用super关键字来访问它。

如果子类被当作父类使用,则通过子类访问的字段是父类的!

用多态的方法模拟ATM操作流程。

程序源代码:

  1 package ATM;
  2 import java.util.Scanner;
  3 abstract class drawmoney{
  4        public abstract double getMoney(double a,double n);
  5 }
  6 class drawmoney100 extends drawmoney{
  7        public double getMoney(double a,double n){
  8               if(a<100){
  9                      System.out.println("余额不足,请选择其他业务");
 10               return a;
 11               }
 12               else {
 13                      System.out.println("已吐出100元");
 14                      return a-=100;
 15               }
 16        }
 17 }
 18 class drawmoney500 extends drawmoney{
 19        public double getMoney(double a,double n){
 20               if(a<500){
 21                      System.out.println("余额不足,请选择其他业务");
 22               return a;
 23               }
 24               else {
 25                      System.out.println("已吐出500元");
 26                      return a-=500;
 27               }
 28        }
 29 }
 30 class drawmoney1000 extends drawmoney{
 31        public double getMoney(double a,double n){
 32               if(a<1000){
 33                      System.out.println("余额不足,请选择其他业务");
 34               return a;
 35               }
 36               else {
 37                      System.out.println("已吐出1000元");
 38                      return a-=1000;
 39               }
 40        }
 41 }
 42 class drawmoney1500 extends drawmoney{
 43        public double getMoney(double a,double n){
 44               if(a<1500){
 45                      System.out.println("余额不足,请选择其他业务");
 46               return a;
 47               }
 48               else {
 49                      System.out.println("已吐出1500元");
 50                      return a-=1500;
 51               }
 52        }
 53 }
 54 class drawmoney2000 extends drawmoney{
 55        public double getMoney(double a,double n){
 56               if(a<2000){
 57                      System.out.println("余额不足,请选择其他业务");
 58               return a;
 59               }
 60               else {
 61                      System.out.println("已吐2000元");
 62                      return a-=2000;
 63               }
 64        }
 65 }
 66 class drawmoney5000 extends drawmoney{
 67        public double getMoney(double a,double n){
 68               if(a<5000){
 69                      System.out.println("余额不足,请选择其他业务");
 70               return a;
 71               }
 72               else {
 73                      System.out.println("已吐出5000元");
 74                      return a-=5000;
 75               }
 76        }
 77 }
 78 class drawmoneyn extends drawmoney{
 79        public double getMoney(double a,double n){
 80               if(a<n){
 81                      System.out.println("余额不足,请选择其他业务");
 82               return a;
 83               }
 84               else {
 85                      System.out.println("已吐出"+n+"元");
 86                      return a-=n;
 87               }
 88        }
 89 }
 90 class Account{
 91        String account;
 92        String usename;
 93        int password;
 94        String date;
 95        double balance;
 96        int Operation;
 97        public Account(){
 98                account="accout1805231593";
 99                usename="王凤彬";
100                password=521114;
101                date="1996.11.11";
102                balance=230.15;
103                Operation=0;
104        }
105        public Account(String a,String b,int c,String d,double e,int f){
106                account=a;
107                usename=b;
108                password=c;
109                date=d;
110                balance=e;
111                Operation=f;
112        }
113 }
114 public class ATM {
115  
116         public Account h=new Account();
117         public boolean shurumina(int a){
118                if(a==h.password) { 
119                System.out.println("密码正确");
120                return true;}
121                else System.out.println("密码错误,请重新输入");
122                return false;
123         }
124         public void jiemian(boolean a){
125                Scanner cin=new Scanner(System.in);
126        while(a){
127                  System.out.println("1.存款");
128                      System.out.println("2.取款");
129                      System.out.println("3.转账汇款");
130                      System.out.println("4.修改密码");
131                      System.out.println("5.查询余额");
132                      System.out.println("6.退卡");
133                      int b=cin.nextInt();
134                      switch (b){
135                      case 1:deposit(b);break;
136                      case 2:withdrawals(b);break;
137                      case 3:transfer(b);break;
138                      case 4:Passwordservice(b);break;
139                      case 5:System.out.println(h.balance);break;
140                      case 6:a=false;break;
141                      }
142        }
143                      
144 }
145         public int withdrawals(int a){
146                Scanner cin=new Scanner(System.in);
147                      System.out.println("1.100元");
148                      System.out.println("2.500元");
149                      System.out.println("3.1000元");
150                      System.out.println("4.1500元");
151                      System.out.println("5.2000元");
152                      System.out.println("6.5000元");
153                      System.out.println("7.其他金额");
154                      System.out.println("8.返回");
155                      int b=cin.nextInt();
156                      double c=0;
157                      drawmoney m=null;
158                      switch (b){
159                      case 1:m= new drawmoney100();m.getMoney(h.balance, b);break;
160                      case 2:m= new drawmoney500();m.getMoney(h.balance, b);break;
161                      case 3:m= new drawmoney1000();m.getMoney(h.balance, b);break;
162                      case 4:m= new drawmoney1500();m.getMoney(h.balance, b);break;
163                      case 5:m= new drawmoney2000();m.getMoney(h.balance, b);break;
164                      case 6:m= new drawmoney5000();m.getMoney(h.balance, b);break;
165                      case 7:{
166                             System.out.println("输入金额");
167                             c=cin.nextDouble();
168                             m= new drawmoneyn();
169                             m.getMoney(h.balance, c);break;                  }
170                      case 8: return a;
171                      }
172                return a;
173         }
174        public int deposit(int a){
175                System.out.println("放入人民币");
176                System.out.println("输入金额");
177                Scanner cin=new Scanner(System.in);
178                int b=cin.nextInt();
179                h.balance+=b;
180                System.out.println("已成功");
181                jiemian(true);
182                return a;
183         }
184         public int transfer(int a){
185                System.out.println("请输入银行账号");
186                Scanner cin=new Scanner(System.in);
187               String b=cin.next();
188               System.out.println("输入转账的金额");
189               int c=cin.nextInt();
190               h.balance-=c;
191               System.out.println("已成功");
192               jiemian(true);
193               return 0;
194         }
195         public int Passwordservice(int a){
196                System.out.println("请输入新密码");
197                Scanner cin=new Scanner(System.in);
198               int b=cin.nextInt();
199               h.password-=b;
200               System.out.println("已成功");
201               return 0;
202         }
203        public static void main(String[] args) {
204               // TODO Auto-generated method stub
205               ATM mm=new ATM();
206               
207               String out=new String();
208               out+="请插入银行卡";
209               System.out.println(out);
210               System.out.println("已成功");
211               out+="请输入密码";
212               System.out.println(out);
213               Scanner cin=new Scanner(System.in);
214               int a=cin.nextInt();
215               while (!mm.shurumina(a)){
216                       a=cin.nextInt();
217                       
218               }
219            mm.jiemian(true);
220               
221        }
222  
223 }

程序分析:ATM机程序的取款进行多态重写,采用了父类调用子类的方法进行重写。

程序截图:

技术分享

课后作业

标签:编译错误   end   passwords   隐藏   osi   ring   tm机   return   考核   

原文地址:http://www.cnblogs.com/wangfengbin/p/6079065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!