« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| 公告 |
不知不觉6年了
|
Blog信息 |
blog名称:〾堯仸〾的天空 日志总数:139 评论数量:503 留言数量:16 访问次数:2718383 建立时间:2005年4月23日 |

| |
Java的继承性和多态(java 训练营) 原创空间, 读书笔记, 软件技术
〾堯仸〾 发表于 2006/12/24 17:20:59 |
001 Java的继承性和多态002 ps :上课没认真听,下午好痛苦!~003 目标004 005 掌握类继承的概念006 熟练进行面向对象的设计007 了解抽象类的概念008 了解Final类的概念009 010 要点011 012 理解继承013 类 jave.lang.Object014 final类和方法015 abstract类和方法016 017 018 运行时多态 exp1:019 020 class A {021 void callme() {022 System.out.println("Inside A's callme() method");023 }024 }025 class B extends A {026 void callme() {027 System.out.println("Inside B's callme() method");028 }029 }030 public class Dispatch {031 public static void main(String [] args) {032 A a = new B();033 a.callme();034 } 035 }036 /**037 038 F:\yach\test\lesson5>javac Dispatch.java039 040 F:\yach\test\lesson5>java Dispatch041 Inside B's callme() method042 043 F:\yach\test\lesson5> */044 045 046 instanceof exp 047 ps:我自己想的,不知道对不对, 上课没认真听^^.048 049 050 051 052 /////////////////////////////////////////////////////////053 public class Employee {054 public void show() {055 System.out.println("Employee");056 }057 }058 059 /////////////////////////////////////////////////////////060 public class Manager extends Employee {061 public void show(){062 System.out.println("Manager show");063 }064 }065 /////////////////////////////////////////////////////////066 public class Engineer extends Employee {067 public void show () {068 System.out.println("Engineer ");069 }070 }071 /////////////////////////////////////////////////////////072 public class TestInstanceof {073 public void doSomething(Employee e) {074 if(e instanceof Manager) 075 System.out.println(e + "@@@@ is Manager");076 else if (e instanceof Engineer)077 System.out.println(e + "**** is Engineer");078 else 079 System.out.println(e + "&&&& is Employee");080 }081 void printClassName(Object obj) {082 System.out.println("This Object name is " + obj.getClass().getName());083 }084 public static void main(String [] args) {085 Employee e1 = new Employee();086 Manager m1 = new Manager();087 Engineer en1 = new Engineer();088 TestInstanceof ti = new TestInstanceof();089 ti.printClassName(e1);090 ti.doSomething(e1);091 ti.printClassName(en1);092 ti.doSomething(en1);093 ti.printClassName(m1);094 ti.doSomething(m1);095 096 }097 }098 099 100 //101 //// final exp ,102 //103 class Value {104 int i = 1 ;105 }106 107 public class FinalData {108 final int i1 = 9 ;109 static final int I2 = 99;110 public static final int I3 = 39;111 Value v1 = new Value();112 final Value v2 = new Value();113 static final Value v3 = new Value();114 final int i4 = (int)(Math.random() * 20);115 static final int i5 = (int)(Math.random() * 20); 116 final int [] a = {1,2,3, 4, 5, 6};117 public void print(String id) {118 System.out.println(119 id + ":" + "i4=" + i4 + "i5=" + i5 );120 }121 public static void main(String[] args) {122 FinalData fd1 = new FinalData();123 124 /**125 fd1.i1 ++;126 fd1.v2 = new Value(); 127 128 F:\yach\test\lesson5>javac FinalData.java129 FinalData.java:16: 无法为最终变量 i1 指定值130 fd1.i1 ++;131 ^132 FinalData.java:17: 无法为最终变量 v2 指定值133 fd1.v2 = new Value();134 ^135 2 错误*/136 //fd1.v3 = new Value();137 fd1.print("fd1");138 System.out.println("Createing new final");139 FinalData fd2 = new FinalData();140 fd1.print("fd1");141 fd2.print("fd2");142 /**143 F:\yach\test\lesson5>java FinalData144 fd1:i4=16i5=16145 Createing new final146 fd1:i4=16i5=16147 fd2:i4=2i5=16148 */149 }150 }151 152 抽象类 abstact 153 ///////////////////////////////////////154 abstract class Point {155 int x = 1;156 int y = 1;157 void move(int dx, int dy) {158 x += dx;159 y += dy;160 alert();161 }162 abstract void alert();163 }164 165 ////////////////////////////////////////166 public class SimplePoint extends Point {167 public void alert() {168 System.out.println(this.x + "*************" + this.y);169 }170 public static void main(String [] args) {171 Point p = new SimplePoint();172 p.move(1, 2);173 } 174 } |
|
» 1 »
|