标签:接口 i++ ntc blog 构造 pre .com uga pig
一、动手动脑
1)源代码
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); } }
2)实验截图:
3)结果分析:
当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定。
这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。
如果子类与父类有相同的字段,则子类中的字段会代替或隐藏父类的字段,子类方法中访问的是子类中的字段(而不是父类中的字段)。如果子类方法确实想访问父类中被隐藏的同名字段,可以用super关键字来访问它。
如果子类被当作父类使用,则通过子类访问的字段是父类的!
二.动手动脑 动物园(使用接口代替抽象基类)
import java.util.Vector; public class zoo4 { 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()); } 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(); } } } interface Animal { public abstract void eat(); } class Lion implements Animal { public void eat() { System.out.println("狮子。"); } } class Monkey implements Animal { public void eat() { System.out.println("猴子。"); } } class Pigeon implements Animal { public void eat() { System.out.println("鸽子。"); } }
三.动手动脑
import java.util.*; public class Ex1115 { public static void main(String[] args) { Account a = new Account("11111111111","dsdf","20161115","256398",23.6); a.jiemian(); } } class Account { private static final String JOptionPane = null; private static String Id; private static String name; private static String date; private static String mima; private static double extra; Account(String i,String n,String d,String m,double e)//构造函数 { Id=i; name=n; date=d; mima=m; extra=e; } public void set_Id(String i){ Id=i; } public void set_name(String n){ name=n;; } public void set_date(String d){ date=d; } public void set_mima(String m){ mima=m; } public void set_extra(double e){ extra=e; } public String get_Id(){ return Id; } public String get_name(){ return name; } public String get_date(){ return date; } public String get_mima(){ return mima; } public double get_extra(){ return extra; } public static void jiemian()//界面 { System.out.println("请选择界面(1.中文2.英文):"); Scanner sc=new Scanner(System.in); String num1=sc.nextLine(); int num11=Integer.parseInt(num1); if(num11==1){ Scanner sc1=new Scanner(System.in); int num=Integer.parseInt(sc1.nextLine()); while(true) { 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.退卡"); if(num==1) qukuan(); else if(num==2) cunkuan(); else if(num==3) zhuan(); else if(num==4) xiugai(); else if(num==5) chaxun(); else if(num==6){ System.out.println("退卡成功!"); } } } else{ } } public static void cunkuan()//存款 { System.out.println("请输入存款金额:"); Scanner sc = new Scanner(System.in); int jine = Integer.parseInt(sc.nextLine()); extra += jine; System.out.println("您现在的金额为:" + extra); } public static void qukuan()//取款 { System.out.println("请输入取款金额"); String output=""; output += "100 " + "\n500 " + "\n1000 " + "\n1500 " + "\n2000 " + "\n5000 " + "\n其他金额(请输入0)" + "\n返回操作(请输入-1) " + "\n退卡(请输入-2)"; System.out.println(output); Scanner sc=new Scanner(System.in); int num=Integer.parseInt(sc.nextLine()); switch(num) { case 100:{ } extra -= 100;break; case 500: extra -= 500;break; case 1000: extra -= 1000;break; case 1500: extra -= 1500;break; case 2000: extra -= 2000;break; case 5000: extra -= 5000;break; case 0: { System.out.println("请输入取款金额:"); Scanner sc1=new Scanner(System.in); int jine=Integer.parseInt(sc1.nextLine()); extra -= jine; sc1.close(); break; } case -1: System.out.println("已返回操作界面");break; case -2: System.out.println("退卡成功!");break; } } public static void zhuan()//转账汇款 { System.out.println("请输入转账行号:"); Scanner sc = new Scanner(System.in); String hanghao = sc.nextLine(); Scanner sc1 = new Scanner(System.in); int jine = Integer.parseInt(sc1.nextLine()); extra -= jine; System.out.println("转账成功!"); } public static void xiugai()//修改密码 { System.out.println("请输入要修改的密码:"); Scanner sc1 = new Scanner(System.in); String mi = sc1.nextLine(); mima = mi; } public static void chaxun()//查询余额 { System.out.println("余额为:"+extra); } }
标签:接口 i++ ntc blog 构造 pre .com uga pig
原文地址:http://www.cnblogs.com/limu/p/6079514.html