Java的动手动脑(五)

日期:2018.11.1

星期四

博客期:021

  Part1: 运行代码

 1 class Grandparent 
 2 {
 3     public Grandparent()
 4      {
 5         System.out.println("GrandParent Created.");
 6     }
 7     public Grandparent(String string) 
 8     {
 9         System.out.println("GrandParent Created.String:" + string);
10     }
11 }
12 
13 class Parent extends Grandparent
14 {
15     public Parent()
16     {
17         //super("Hello.Grandparent.");
18         System.out.println("Parent Created");    
19         //super("Hello.Grandparent.");
20       }
21 }
22 
23 class Child extends Parent 
24 {
25     public Child()
26     {
27         System.out.println("Child Created");
28     }
29 }
30 
31 
32 
33 public class TestInherits 
34 {
35     public static void main(String args[])
36     {
37         Child c = new Child();
38     }
39 
40 }
TestInherits.java

  运行结果如下:

  运行结果说明:java的构造方法与C++的构造函数的构造顺序相同!它的构造也是从上到下的(由基类开始构造)!而且顺序不可变,这个问题我们一会儿会再提到!其实就是构造方法实际上就是给类的对象一个初始化的方法!如果我们先初始化子类的方法,子类的方法要调用父类的数据成员,这不就尴尬了吗?你让人家子类上哪儿去找啊?所以顺序就认为规定不变,即使你这个父类是个“空类”(实际上Java的类中是不存在空类的)。

  Part2: 运行代码

 1 package Test;
 2 public class ExplorationJDKSource {
 3 
 4     /**
 5      * @param args
 6      */
 7     public static void main(String[] args) {
 8         System.out.println(new A());
 9     }
10 
11 }
12 
13 class A{}
ExplorationJDKSource.java

  运行结果如下:

  运行结果说明:其实之所以我们的类可以做到输出,是因为除Object类以外,我们的写每一个类,都是有它的父类的,默认继承Object类!所以Object类里的方法名称都得到了继承!而System.out实际上是Object的一个子类PrintStream,而这个子类内部定义了一个public static void println(String ...);的方法传进来的大类参数就会调用父类的toString方法,改成String类型并输出。而该方法如下:

  而其中的hashCode()是本地的方法,用于生成随机数!

  Part 2.5: 顺便说一下今天学到的判断类型的方法!

  Object hello = "Hello,酷狗!";

  (hello instanceof Math) 这一部分返回boolean类型的值;返回hello是否属于Math类型的值!

  Part 3: 运行代码

 1 public class ParentChildTest {
 2     public static void main(String[] args) {
 3         Parent parent=new Parent();
 4         parent.printValue();//Parent.printValue(),myValue=100
 5         Child child=new Child();
 6         child.printValue();//Child.printValue(),myValue=200
 7         
 8         parent=child;
 9         parent.printValue();//Child.printValue(),myValue=200
10         
11         parent.myValue++;
12         parent.printValue();//Child.printValue(),myValue=200
13         
14         ((Child)parent).myValue++;
15         parent.printValue();//Child.printValue(),myValue=201
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 }
ParentChildTest.java

  运行结果:

  运行结果说明:我们先肯定一点——就是说一定是只有子类或它自己可以直接初始化父类!强制类型转换也是如此!只能将子类强制专成父类,其他的转型要通过类的成员方法来完成!这里可能不太好懂,那我就仔细的逐句给大家分析一下!首先!类文件我们就跳过了!(-v-!)嗯,这两个类文件就是初始化,也没什么难处!而且前两个printValue()方法应该也没有什么疑问!就是调用自己方法嘛!我们可以清楚的知道子类覆盖了父类的方法,所以调用还是调用的自己的!parent=child;这句是把child初始化parent的!就是拿child的数据来初始化parent!自然parent.printValue(); 一句调用方法,也就是调用子类方法!再说一下就是当前类型是什么,调用的就是哪个数据!现在child已经赋值给parent了,再运行自然显示200,而parent.myValue++;一句加的是parent所在的value而不是child的,所以200不变;再然后是把parent转成了Child类型,之后再调用value就是子类的value了,这样++语句就会生效,进而得到了一个201的结果。

 

posted @ 2018-11-01 17:29  初等变换不改变矩阵的秩  阅读(313)  评论(0)    收藏  举报