Java学习笔记三

来源:互联网 发布:php框架必须学码 编辑:程序博客网 时间:2024/06/05 14:41

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)
*///:~

再转载一个例子:

  1. class Parent{  
  2.     static String name = "hello";  
  3.     static {  
  4.         System.out.println("parent static block");  
  5.     }  
  6.     public Parent(){  
  7.         System.out.println("parent constructor");  
  8.     }  
  9. }  
  10.   
  11. class Child extends Parent{  
  12.     static String childName = "hello";  
  13.     static {  
  14.         System.out.println("child static block");  
  15.     }  
  16.     public Child(){  
  17.         System.out.println("child constructor");  
  18.     }  
  19. }  
  20.   
  21. public class StaticIniBlockOrderTest {  
  22.   
  23.     public static void main(String[] args) {  
  24.         new Child();//语句(*)  
  25.     }  
  26. }  
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关键子来调用父类带参数的构造方法,否则编译不能通过。