⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 declarations and access control (2).txt

📁 收集来的一些java方面的技术
💻 TXT
📖 第 1 页 / 共 2 页
字号:
only by static method calls.
Cannot pass on the checked exceptions. Must catch and handle them.
2.    Static initializer blocks.
       Used to initialize static variables and load native libraries.
Cannot pass on the checked exceptions. Must catch and handle them.
3.    Instance initializer blocks.
       Used to factor out code that is common to all the constructors.
       Also useful with anonymous classes since they cannot have constructors.
       All constructors must declare the uncaught checked exceptions, if any.
       Instance Initializers in anonymous classes can throw any exception.(?)
·    In all the initializers, forward referencing of variables is not allowed. Forward referencing of methods is allowed.
·    Order of code execution (when creating an object) is a bit tricky.
1.    static variables initialization.
2.    static initializer block execution. (in the order of declaration, if multiple blocks found)
3.    constructor header ( super or this – implicit or explicit )
4.    instance variables initialization / instance initializer block(s) execution
5.    rest of the code in the constructor

Objective 4 
State the legal return types for any method given the declarations of all related methods in this or parent classes.
See mynote_6 for overloeading and overridden.
Examples:
Q1.
Assume we have the following code in the file /abc/def/Q.java: 
//File: /abc/def/Q.java:
package def;

public class Q {
   private int privateVar;
   int packageVar;
   protected int protectedVar;
   public int publicVar;
}
and this code is in /abc/Sub.java: 
//File: /abc/Sub.java:
public class Sub extends Tester {
}
and this code is in /abc/Tester.java: 
//File: /abc/Tester.java:
import def.Q;

public class Tester extends Q {
   Q q = new Q();
   Sub sub = new Sub();

   public void someMethod() {
      // First, try to refer to q's memebers.
      q.privateVar = 1;    // compiler error
      q.packageVar = 2;    // compiler error
      q.protectedVar = 3;  // compiler error
      q.publicVar = 4;     // fine

      // Next, try to refer to this object's members
      // supplied by class Q.
      privateVar = 5;        // compiler error
      packageVar = 6;        // compiler error
      protectedVar = 7;      // fine
      publicVar = 8;         // fine

      // Next, let's try to access the members of
      // another instance of Tester.
      Tester t = new Tester();
      t.privateVar = 9;      // compiler error
      t.packageVar = 10;     // compiler error
      t.protectedVar = 11;   // fine
      t.publicVar = 12;      // fine

      // Finally, try to refer to the members in a
      // subclass of Tester.
      sub.privateVar = 13;   // compiler error
      sub.packageVar = 14;   // compiler error
      sub.protectedVar = 15; // fine
      sub.publicVar = 16;    // fine
   }
}
Q2
Determine the result of attempting to compile and run the following code:
class A {
   public A() {
      System.out.println("AAA");
   }
   {
      System.out.println("456");
   }
}

public class B extends A {
   B() {
      this(12);
      System.out.println("BBB");
   }
   B(int x) {
      System.out.println("CCC");
   }
   {
      System.out.println("123");
   }
   public static void main(String[] args) {
      new B();
   }
}

The output is: 
456
AAA
123
CCC
BBB
Q3
Determine the result of attempting to compile and run the following code: 
class A {
   public int Avar;
   public A() {
      System.out.println("AAA");
      doSomething();
   }
   public void doSomething() {
      Avar = 1111;
      System.out.println("A.doSomething()");
   }
}

public class B extends A {
   public int Bvar = 2222;
   public B() {
      System.out.println("BBB");
      doSomething();
      System.out.println("Avar=" + Avar);
   }
   public void doSomething() {
      System.out.println("Bvar=" + Bvar);
   }
   public static void main(String[] args) {
      new B();
   }
}

The output is: 
AAA
Bvar=0
BBB
Bvar=2222
Avar=0
Q4
A class is not abstract if it implements all superinterfaces either directly or inherited
from superclass.

eg. class a { void method() {} }
interface ab { void method(); }
class abc extends a implements ab {} // PERFECTLY LEGAL since superclass has 
implemented the interface method signature.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -