Java11月18日
动手动脑
实验一:
1. 下边的程序运行结果是什么?
2.你如何解释会得到这样的输出?
3.计算机是不会出错的,之所以得到这样的运行结果也是有原因的那么从这些运行结果中,你能总结出Java的哪些语法特性?
程序源代码:
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=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);
}
}
(1)实验结果运行图:
(2)这样结果输出的原因:
答:首先前两个分别引用各自的类的变量,所以分别是100和200没有什么异议,第三个把子类的赋值给父类了,所以输出结果为200,第四个因为只是改变了弗雷德变量值,没有改变子类的变量值,所以仍为200,而此时父类的为101。最后对子类进行强制转换,所以变量值也发生了改变为201.
(3)当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。
这个特性实际上就是面向对象“多态”特性的具体表现。
实验二:多态方法模拟ATM程序
//ATM
//zhanxinwu,November ,15,2016.
import java.util.Scanner;
abstract class QuKuan
{
abstract int qukuan();
}
class Account extends QuKuan
{
String ID;
String name;
String Date;
int operate;
String mima;
double yue;
Account()
{
ID="1234567891a";
mima="123456";
yue=0;
}
int qukuan()
{
int flag=0;
double qu=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");
System.out.println("7 其他金额");
System.out.println("8 退卡");
System.out.println("9 返回上一界面");
System.out.print("请选取要取款的金额'操作数':");
Scanner scanner=new Scanner(System.in);
operate=scanner.nextInt();
while(operate<1||operate>9)
{
System.out.print("请重新选取要取款的金额‘操作数’:");
operate=scanner.nextInt();
}
switch(operate){
case 1:qu=100;
case 2:qu=500;
case 3:qu=1000;
case 4:qu=1500;
case 5:qu=2000;
case 6:qu=5000;
case 7:{System.out.print("请输入要取款的金额:");
qu=scanner.nextDouble();
if(yue<qu)
{
System.out.println("余额不足!");
}
else
{
yue-=qu;
System.out.println("取款成功,余额为:"+yue);
}
}
}
return flag;
}
void xiugaimima()
{
System.out.print("请输入要修改的取款账号所对应的密码:");
Scanner scanner=new Scanner(System.in);
String mi=scanner.next();
mima=mi;
System.out.println("修改后的取款账号所对应的密码为:"+mima);
}
void cunkuan()
{
System.out.print("请输入要存款的金额:");
Scanner scanner=new Scanner(System.in);
double yu=scanner.nextDouble();
yue+=yu;
System.out.println("存款成功,余额为:"+yue);
}
void chaxun()
{
System.out.println("余额为:"+yue);
}
void zhuanzhang()
{
System.out.print("请输入转入的行号:");
Scanner scanner=new Scanner(System.in);
String hang=scanner.next();
System.out.print("请输入要转账的金额:");
double yu=scanner.nextDouble();
if(yue<yu)
{
System.out.println("余额不足,请充值!!!");
}
else
{
yue-=yu;
System.out.println("转账成功,余额为:"+yue+",已转入到"+hang+"中"+yu+"元");
}
}
}
public class ATMduo
{
public static void main(String[] args)
{
int k;
Scanner scanner=new Scanner(System.in);
Account m=new Account();
while(true)
{
System.out.print("请输入密码:");
String n=scanner.next();
if(n.equals(m.mima))
{
while(true)
{
System.out.println("1 存款");
System.out.println("2 取款");
System.out.println("3 转账汇款");
System.out.println("4 修改密码");
System.out.println("5 查询余额");
System.out.println("6 退出系统");
System.out.print("请输入操作项:");
k=scanner.nextInt();
if(k==1) m.cunkuan();
if(k==2)
{
int g=m.qukuan();
if(g==1) break;
}
if(k==3) m.zhuanzhang();
if(k==4) m.xiugaimima();
if(k==5) m.chaxun();
if(k==6) break;
}
}
else System.out.println("该卡已被锁定,请联系银行工作人员!!!");
}
}
}
Java11月18日的更多相关文章
- 2016年12月18日 星期日 --出埃及记 Exodus 21:13
2016年12月18日 星期日 --出埃及记 Exodus 21:13 However, if he does not do it intentionally, but God lets it hap ...
- 2015年8月18日,杨学明老师《技术部门的绩效管理提升(研讨会)》在中国科学院下属机构CNNIC成功举办!
2015年8月18日,杨学明老师为中国网络新闻办公室直属央企中国互联网络中心(CNNIC)提供了一天的<技术部门的绩效管理提升(研讨会)>培训课程.杨学明老师分别从研发绩效管理概述.研发绩 ...
- 2016年11月18日 星期五 --出埃及记 Exodus 20:9
2016年11月18日 星期五 --出埃及记 Exodus 20:9 Six days you shall labor and do all your work,六日要劳碌作你一切的工,
- 2016年10月18日 星期二 --出埃及记 Exodus 19:2
2016年10月18日 星期二 --出埃及记 Exodus 19:2 After they set out from Rephidim, they entered the Desert of Sina ...
- 天津Uber优步司机奖励政策(1月18日~1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 长沙Uber优步司机奖励政策(1月18日~1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 西安Uber优步司机奖励政策(1月18日~1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 上海Uber优步司机奖励政策(1月18日~1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Git学习(二)(2015年11月18日)(2016年1月29日)
2015年11月18日Git学习: .Shell 删除文件夹及其所有文件 rd/s/q 文件目录 ---------------当前为先创建本地Git库后与网上Git服务器关联------------ ...
随机推荐
- c语言实现配置文件的读写
配置文件的格式如下: key1 = value1 key2 = value2 . . . 名值对以一个=链接,一条记录以换行符分割 头文件: #include<stdio.h> #incl ...
- springcloud超简单的入门2--Eureka服务治理
Eureka服务治理 下面请听第一个话题,母...咳咳,拿错书了. Eureka简介 eureka是什么呢? 简单来说呢,当我的微服务应用多了起来,一个一个写死再程序里是件很不优雅的事情,而且同一服务 ...
- Tomcat9控制台中文乱码的解决方案
1.网上大部分都是这种方法 注释掉 tomcat 9 安装目录下的conf里的 logging.properties 找到 java.util.logging.ConsoleHandler.encod ...
- 每个Java开发人员都应该知道的10个基本工具
大家好,我们已经在2019年的第9个月,我相信你们所有人已经在2019年学到了什么,以及如何实现这些目标.我一直在写一系列文章,为你提供一些关于你可以学习和改进的想法,以便在2019年成为一个更好的. ...
- IBM MQ reason code list
The reason code parameter (Reason) is a qualification to the completion code parameter (CompCode). I ...
- jmeter性能分析
1.硬件要求:包括客户端和服务端的cpu,mem,network,disk等,这些硬件设备必须满足性能测试的前提下,才能进行性能测试,否则得到的各项指标不一定是正确的 2.场景分析: 测试前的准备工作 ...
- input和btton的相互使用————小程序
input和btton的相互使用----小程序 index.js data: { userxx:'1111', }, changeSum(){ // this.data.userxx="ch ...
- PTA A1011&A1012
A1011 World Cup Betting (20 分) 题目内容 With the 2010 FIFA World Cup running, football fans the world ov ...
- 42 (OC)* 字典实现原理--哈希原理
一.NSDictionary使用原理 1.NSDictionary(字典)是使用 hash表来实现key和value之间的映射和存储的,hash函数设计的好坏影响着数据的查找访问效率. - (void ...
- [Leetcode] 第309题 最佳买卖股票时机含冷冻期
一.题目描述 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 . 设计一个算法计算出最大利润.在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票): 你不能同时参与 ...