Java學習筆記三
Java訪問控制級別:
The levels of access control from “most access” to “least access” are public, protected, package access (which has no keyword), and private.
訪問控制符 | 同類 | 同包子類 | 同包其它類 | 不同包子類 | 不同包其它類 |
public | √ | √ | √ | √ | √ |
protected | √ | √ | √ | √ | × |
預設 | √ | √ | √ | × | × |
private | √ | × | × |
A library is a group of these class files. Each source file usually has a public class and any number of non-public classes, so there’s one public component for each source file. If you want to say that all these components (each in its own separate .java and .class files) belong together, that’s where the package keyword comes in.
Package access:It means that all the other classes in the current package have access to that member, but to all the classes outside of this package, the member appears to be private.
public access:
一個類申明為public,但是其中某個方法並不是public的,則這個方法在不同包中的類中是不可見的,但是在同包的類中是可見的,實際上void bite()是包控制許可權
package access.dessert;
public class Cookie {
public Cookie() {
System.out.println("Cookie constructor");
}
void bite() { System.out.println("bite"); }
} ///:~
package access;
import access.dessert.*;
public class Dinner {
public static void main(String[] args) {
Cookie x = new Cookie();
x.bite(); // Can’t access
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method bite() from the type Cookie is not visible
at access.Dinner.main(Dinner.java:7)
The private keyword means that no one can access that member except the class that contains that member, inside methods of that class. Other classes in the same package cannot access private members, so it’s as if you’re even insulating the class against yourself
出來混遲早是要還的,以前java沒學好,現在好好學java。鬱悶呀
Each overloaded method must take a unique list of argument types.
過載僅返回值型別不同是會出問題的,需要保證引數列表不同
When you don’t put in any constructors, it’s as if the compiler says, “You are bound to need some constructor, so let me make one for you.” But if you write a constructor, the compiler says, “You’ve written a constructor so you know what you’re doing; if
you didn’t put in a default it’s because you meant to leave it out.”
object. In a constructor, the this keyword takes on a different meaning when you give it an argument list. It makes an explicit call to the constructor that matches that argument list.
//: initialization/Flower.java
// Calling constructors with "this"
import static net.mindview.util.Print.*;
public class Flower {
int petalCount = 0;
String s = "initial value";
Flower(int petals) {
petalCount = petals;
print("Constructor w/ int arg only, petalCount= "
+ petalCount);
}
Flower(String ss) {
print("Constructor w/ String arg only, s = " + ss);
s = ss;
}
Flower(String s, int petals) {
this(petals);
//! this(s); // Can’t call two!
this.s = s; // Another use of "this"
print("String & int args");
}
Flower() {
this("hi", 47);
print("default constructor (no args)");
}
void printPetalCount() {
//! this(11); // Not inside non-constructor!
print("petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.printPetalCount();
}
} /* Output:
Constructor w/ int arg only, petalCount= 47
String & int args
default constructor (no args)
petalCount = 47 s = hi
*///:~
摘自think in java
The meaning of static
With the this keyword in mind, you can more fully understand what it means to make a method static. It means that there is no this for that particular method. You cannot call non-static methods from inside static methods2 (although the reverse is possible),
and you can call a static method for the class itself, without any object. In fact, that’s primarily what a static method is for. It’s as if you’re creating the equivalent of a global method. However, global methods are not permitted in Java, and putting the
static method inside a class allows it access to other static methods and to static fields.
Some people argue that static methods are not object-oriented, since they do have the semantics of a global method; with a static method, you don’t send a message to an object, since there’s no this. This is probably a fair argument, and if you find yourself
using a lot of static methods, you should probably rethink your strategy. However, statics are pragmatic, and there are times when you genuinely need them, so whether or not they are “proper OOP” should be left to the theoreticians.
Within a class, the order of initialization is determined by the order that the variables are defined within the class.
靜態物件的初始化順序:
基本按照申明的順序來,但是有靜態物件的宣告的時候,先初始化靜態物件。
直接看例子吧:// Specifying initial values in a class definition.
import static net.mindview.util.Print.*;
class Bowl {
Bowl(int marker) {
print("Bowl(" + marker + ")");
}
void f1(int marker) {
print("f1(" + marker + ")");
}
}
class Table {
static Bowl bowl1 = new Bowl(1);
Table() {
print("Table()");
bowl2.f1(1);
}
void f2(int marker) {
print("f2(" + marker + ")");
}
static Bowl bowl2 = new Bowl(2);
}
class Cupboard {
Bowl bowl3 = new Bowl(3);
static Bowl bowl4 = new Bowl(4);
Cupboard() {
print("Cupboard()");
bowl4.f1(2);
}
void f3(int marker) {
print("f3(" + marker + ")");
}
static Bowl bowl5 = new Bowl(5);
}
public class StaticInitialization {
public static void main(String[] args) {
print("Creating new Cupboard() in main");
new Cupboard();
print("Creating new Cupboard() in main");
new Cupboard();
table.f2(1);
cupboard.f3(1);
}
static Table table = new Table();
static Cupboard cupboard = new Cupboard();
} /* Output:
Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
f2(1)
f3(1)
*///:~
再轉載一個例子:
- class Parent{
- static String name = "hello";
- static {
- System.out.println("parent static block");
- }
- public Parent(){
- System.out.println("parent constructor");
- }
- }
- class Child extends Parent{
- static String childName = "hello";
- static {
- System.out.println("child static block");
- }
- public Child(){
- System.out.println("child constructor");
- }
- }
- public class StaticIniBlockOrderTest {
- public static void main(String[] args) {
- new Child();//語句(*)
- }
- }
class Parent{ static String name = "hello"; static { System.out.println("parent static block"); } public Parent(){ System.out.println("parent constructor"); } } class Child extends Parent{ static String childName = "hello"; static { System.out.println("child static block"); } public Child(){ System.out.println("child constructor"); } } public class StaticIniBlockOrderTest { public static void main(String[] args) { new Child();//語句(*) } }
問題:當執行完語句(*)時,列印結果是什麼順序?為什麼?
解答:當執行完語句(*)時,結果是這樣一個順序:parent static block,child static block,parent constructor,child constructor。
分析:當執行newChild()時,它首先去看父類裡面有沒有靜態程式碼塊,如果有,它先去執行父類裡面靜態程式碼塊裡面的內容,當父類的靜態程式碼塊裡面的內容執行完畢之後,接著去執行子類(自己這個類)裡面的靜態程式碼塊,當子類的靜態程式碼塊執行完畢之後,它接著又去執行父類的構造方法,父類的構造方法執行完畢之後,它接著再去執行子類的構造方法,這個就是一個物件的初始話化順序。
總結:物件的初始化順序是首先執行父類靜態的內容,父類靜態的內容執行完畢後,接著去執行子類的靜態的內容,當子類的靜態內容執行完畢之後,再去執行父類的構造方法,父類的構造方法執行完畢之後,再去執行子類的構造方法。總之一句話,靜態的塊內容先執行。
注意:子類的構造方法,不管這個構造方法帶不帶引數,預設的它都會先去尋找父類的不帶引數的構造方法。如果父類沒有不帶引數的構造方法,那麼子類必須用supper關鍵子來呼叫父類帶引數的構造方法,否則編譯不能通過。
相關文章
- Java學習筆記-Day48 JavaScript(三)筆記JavaScript
- JAVA學習筆記Java筆記
- 《JAVA學習指南》學習筆記Java筆記
- React 學習筆記【三】React筆記
- cmake 學習筆記(三)筆記
- goLang學習筆記(三)Golang筆記
- unity學習筆記(三)Unity筆記
- ONNXRuntime學習筆記(三)筆記
- Python學習筆記(三)Python筆記
- java學習筆記6Java筆記
- Java學習筆記4Java筆記
- Java JNI 學習筆記Java筆記
- Java 集合學習筆記Java筆記
- Java學習筆記記錄(二)Java筆記
- Redis學習筆記(三) 字典Redis筆記
- TS學習筆記(三):類筆記
- CANopen學習筆記(三)NMT筆記
- c++學習筆記(三)C++筆記
- react native學習筆記(三)React Native筆記
- Java JDK 9學習筆記JavaJDK筆記
- TensorFlow Java API 學習筆記JavaAPI筆記
- Java學習筆記系列-反射Java筆記反射
- 【部分】Java速成學習筆記Java筆記
- Java基礎學習筆記Java筆記
- Java學習筆記--運算子Java筆記
- Kotlin for Java Developers 學習筆記KotlinJavaDeveloper筆記
- 【Java學習筆記】Collections集合Java筆記
- java學習筆記(異常)Java筆記
- (三)Java併發學習筆記–執行緒封閉Java筆記執行緒
- Java設計模式學習筆記(三) 工廠方法模式Java設計模式筆記
- 架構學習筆記系列三架構筆記
- ES6 學習筆記三筆記
- Spark學習筆記(三)-Spark StreamingSpark筆記
- Python爬蟲學習筆記(三)Python爬蟲筆記
- (一)Java併發學習筆記Java筆記
- JAVA 學習併發筆記(一)Java筆記
- 2018.03.12、Java-Thread學習筆記Javathread筆記
- Java學習筆記(七十二)—— CookieJava筆記Cookie
- 阿猛學習筆記java十二筆記Java