您的位置:首页 > 编程语言 > Java开发

JAVA基础知识精华总结

2011-10-12 11:32 567 查看
JAVA基础知识精华总结
1、 对象的初始化
(1) 非静态对象的初始化
在创建对象时,对象所在类的所有数据成员会首先进行初始化。
基本类型:int型,初始化为0。
如果为对象:这些对象会按顺序初始化。
※在所有类成员初始化完成之后,才调用本类的构造方法创建对象。
构造方法的作用就是初始化。
(2) 静态对象的初始化
程序中主类的静态变量会在main方法执行前初始化。
不仅第一次创建对象时,类中的所有静态变量都初始化,并且第一次访问某类(注意此时
未创建此类对象)的静态对象时,所有的静态变量也要按它们在类中的顺序初始化。
2、 继承时,对象的初始化过程
(1) 主类的超类由高到低按顺序初始化静态成员,无论静态成员是否为private。
(2) 主类静态成员的初始化。
(3) 主类的超类由高到低进行默认构造方法的调用。注意,在调用每一个超类的默认构造
方法前,先进行对此超类进行非静态对象的初始化。
(4) 主类非静态成员的初始化。
(5) 调用主类的构造方法。
3、 关于构造方法
(1) 类可以没有构造方法,但如果有多个构造方法,就应该要有默认的构造方法,否则在继承此类时,需要在子类中显式调用父类的某一个非默认的构造方法了。
(2) 在一个构造方法中,只能调用一次其他的构造方法,并且调用构造方法的语句必须是
第一条语句。
4、 有关public、private和protected
(1) 无public修饰的类,可以被其他类访问的条件是:a.两个类在同一文件中,b.两个类
在同一文件夹中,c.两个类在同一软件包中。
(2) protected:继承类和同一软件包的类可访问jdwx
(3) 如果构造方法为private,那么在其他类中不能创建该类的对象。
5、 抽象类
(1) 抽象类不能创建对象。
(2) 如果一个类中一个方法为抽象方法,则这个类必须为abstract抽象类。
(3) 继承抽象类的类在类中必须实现抽象类中的抽象方法。
(4) 抽象类中可以有抽象方法,也可有非抽象方法。抽象方法不能为private。
(5) 间接继承抽象类的类可以不给出抽象方法的定义。
6、 final关键字
(1) 一个对象是常量,不代表不能转变对象的成员,仍可以其成员进行操作。
(2) 常量在使用前必须赋值,但除了在声明的同时初始化外,就只能在构造方法中初始化

(3) final修饰的方法不能被重置(在子类中不能出现同名方法)。
(4) 如果声明一个类为final,则所有的方法均为final,无论其是否被final修饰,但数据
成员可为final也可不是。
7、 接口interface (用implements来实现接口)
(1) 接口中的所有数据均为 static和final即静态常量。尽管可以不用这两个关键字修饰
,但必须给常量赋初值。
(2) 接口中的方法均为public,在实现接口类中,实现方法必须可public关键字。
(3) 如果使用public来修饰接口,则接口必须与文件名相同。
8、 多重继承
(1) 一个类继承了一个类和接口,那么必须将类写在前面,接口写在后面,接口之间用逗
号分隔。
(2) 接口之间可多重继承,注意使用关键字extends。
(3) 一个类虽只实现了一个接口,但不仅要实现这个接口的所有方法,还要实现这个接口
继承的接口的方法,接口中的所有方法均须在类中实现。
9、 接口的嵌入
(1) 接口嵌入类中,可以使用private修饰。此时,接口只能在所在的类中实现,其他类不
能访问。
(2) 嵌入接口中的接口一定要为public。
10、类的嵌入
(1) 类可以嵌入另一个类中,但不能嵌入接口中。
(2) 在静态方法或其他方法中,不能直接创建内部类对象,需通过手段来取得。
手段有两种:
class A {
class B {}
B getB() {
B b = new B();
return b;
}
}
static void m() {
A a = new A();
A.B ab = a.getB(); // 或者是 A.B ab = a.new B();
}
(3) 一个类继承了另一个类的内部类,因为超类是内部类,而内部类的构造方法不能自动
被调用,这样就需要在子类的构造方法中明确的调用超类的构造方法。
接上例:
class C extends A.B {
C() {
new A().super(); // 这一句就实现了对内部类构造方法的调用。
}
}
构造方法也可这样写:
C(A a) {
a.super();
} // 使用这个构造方法创建对象,要写成C c = new C(a); a是A的对象。
11、异常类
JAVA中除了RunTimeException 类,其他异常均须捕获或抛出。

核总结JAVA基础知识
1,一个对象初始化
(1)非静态对象的初始化
创建一个对象,对象类,所有的数据成员初始化第一。
基本类型:int型,初始化为0
如果该项目:这些对象初始化的顺序。
※所有的小组成员初始化完成之前,调用类的构造函数来创建对象。
在Builder的作用已被初始化。
(2)静态对象的初始化
主类节目的主要方法是静态变量之前执行初始化。
不仅第一次创建对象,所有的静态变量的类被初始化,并通过某种类型的第一次访问(注意,目前
不创建这样一个对象)是一个静态武汉家电维修的对象,而且所有的静态变量初始化的顺序在课堂上。
2,继承,对象初始化
(1)类超前高后低,以初始化静态类成员,无论静态成员是私有的。
(2)静态成员初始化部分。
(3)默认情况下,超类的构造函数调用的土地主要类别。请注意,每个超类的默认构造函数的调用
路在本非静态对象的初始化超类。
(4)非静态成员的初始化主要类别。
(5)调用生成器部分。
3,是一个建设者
(1)类的构造函数不可,但如果你有多个建筑工地多,你应该有一个默认的构造函数,否则继承,司显式调用非默认的构造方法的父类。
(2)生成器只能呼吁和解建设者其他构造函数,并调用
在其第一个语句。
4,公共,私人和保护
(1)不公布修改后的类,提供可用于其他类,即:a.为同一个文件的两个类,B类两种
在同一文件夹中,C.两个在一个包中的类。
(2)保护:类继承和类可以用在同一个包。
(3)如果构造函数是私有的,那么其他类可以创建这个类的对象。
5,一个抽象类
(1)抽象类不能创建对象。
(2)如果一个类是一个抽象的方法,这个类是一个抽象类是一个抽象的。
(3)类继承一个抽象类,抽象类必须是抽象类的方法。
(4),抽象类可以有抽象方法,也可以是一个非抽象方法。抽象方法不能是私有的。
(5)间接继承抽象类可以定义抽象方法。
6,final关键字
(1)对象是不变的,并不意味着一个成员不能改变的对象,其成员仍可以工作。
(2)已确定的常量在使用前,但在除了介绍同时消息,但它只能被初始化建设者

(3)改变最终方法不能被重置(不在同一名称的子类方法中)。
(4)如果一个类被声明为final,则所有的方法都是决赛,无论他们最后的变化,但数据
大家也可以说了算。
7,接口连接器(用于实现接口来实现)
(1),所有接口是静态的,最终是一个静态的标准。虽然这两个关键字可以更改,恕不
不过,考虑到不断的初始值。
(2)接口的​​方法是公开的,类实现接口的实现是一个公共字。
(3)如果你改变了公共接口,该接口是作为文件名相同。
8,多重继承
(1)类继承的类和接口,这是编者的类,接口,写回的接口点
用逗号分隔。
(2)可接口之间的多重继承,注意关键字扩展。
(3),即使只有一个类实现一个接口,但没有实现这个接口的所有方法,而且在这方面
从接口继承的方法,接口,在课堂上实施的所有方法。
9,为嵌入式用户界面
(1)接口嵌入在类,你可以使用private修饰符。此时,用户界面​​可以实现只有当类,​​其他类不
访问。
(2)在用户界面中嵌入是公开的。
10,类的嵌入式
(1)类可以嵌入另一个类,而不是在用户界面中嵌入。
(2)静态方法或其他方法不能直接创建对象的类,通过以下方式实现。
有两种方法:
类{
{B}
乙getB(){
新的B B B =();
返回B;
}
}
无效M(){
=新建();
AB AB = a.getB()/ /或AB,AB = a.new B();
}
(3)在一个类继承另一个类的类,因为超类武汉空调维修是一个内部类,但不能在类的构造函数会自动
正所谓,这就要求必须建立显式调用父类的构造司。
与案件有关的:
C类扩展{A.B
C(){
新的()。超级()/ /这是一个内部类来实现构造函数的调用。
}
}
施工方法也可以这样写:
C(){
a.super();
} / /使用此构造函数创建可写入新的C =(),是从事CC对象。
11,用类异常
JAVA的RuntimeException除了上课,或抛出其他异常必须关闭。

JAVA basics essence of summary
1, the object initialization
(1) non-static object initialization
Create the object, the object class where all the data members will first be initialized.
Basic types: int type, initialized to 0.
If an object: These objects will be initialized in order.
※ In all class members initialization is complete before calling the class constructor to create objects.
The role of constructor is initialized.
(2) static object initialization
The main class program will be the main method static variables initialized before execution.
Not only for the first time to create objects, classes of all static variables are initialized, and the first visit to a particular type (note that at this time
Not create such an object) of the static object, but also by all the static variables are initialized in the order in the class.
2, inheritance, object initialization
(1) The main class of ultra-high to low in order to initialize static class members, regardless of whether the static member is private.
(2) main class initialization of static members.
(3) main categories of descending to the default superclass constructor call. Note that each call to super class's default constructor
Method before the superclass for this non-static object initialization.
(4) main categories of non-static member initialization.
(5) call the main class constructor.
3, with regard to construction methods
(1) class constructor can not, but if there are more than one constructor, you should have a default constructor, otherwise be inherited, the subclass needs to explicitly call a parent class of a non-default constructor methods.
(2) in a constructor, one can only call the other constructor, and calls the constructor of the statement must be
The first statement.
4, the public, private and protected
(1) No public modified class, other classes can be accessed on condition that: a. two classes in the same file, b. two classes
In the same folder, c. two classes in the same package.
(2) protected: inheritance class and the class can access the same package.
(3) If the constructor is private, then in the other classes can not create objects of this class.
5, the abstract class
(1) abstract class can not create object.
(2) If a class method is abstract in a way, this class is an abstract class must be abstract.
(3) The class extends an abstract class abstract class must implement the abstract class methods.
(4) abstract class can have abstract methods, may also have non-abstract methods. Abstract method can not be private.
(5) indirectly inherited abstract class can not give the definition of abstract methods.
6, final keyword
(1) an object is constant, does not mean that a member can not change the object, its members can still operate.
(2) constants must be assigned before use, but in a statement at the same time in addition to initialization, but it can only be initialized in the constructor
.
(3) final modification of the method can not be reset (can not appear in the sub-class method the same name).
(4) If a class is declared final, then all methods are final, regardless of their final modification, but the data
Members can also not final.
7, the interface interface (implements used to implement the interface)
(1) all data interfaces are static and final is a static constant. Although these two keywords can be modified without
, But must give a constant initial value.
(2) interface methods are public, the class that implements the interface, implementations must be public keyword.
(3) If the public to modify the interface, the interface must be the same as the file name.
8, multiple inheritance
(1) a class inherits a class and interface, it must be EDITORIAL class, interface, written on the back, the interface between the comma
Comma-separated.
(2) can be multiple inheritance between interfaces, note that using the keyword extends.
(3), although only a class implements an interface, but not to implement all the methods of this interface, but also implement this interface
Inherited interface methods, interfaces, all the methods to be implemented in a class.
9, the interface of the embedded
(1) interfaces embedded class, you can use the private modifier. At this point, the interface can only be implemented where the class, other classes do not
Access.
(2) embedded in the interface the interface must be public.
10, class of embedded
(1) class can be embedded in another class, but not embedded in the interface.
(2) in a static method or other methods, can not directly create objects within the class, to be achieved through the means.
There are two methods:
class A {
class B {}
B getB () {
B b = new B ();
return b;
}
}
static void m () {
A a = new A ();
AB ab = a.getB (); / / or AB ab = a.new B ();
}
(3) a class inherits another class within the class, because the super class is an internal class, but not within the class constructor automatically
Is called, which will require the sub-class constructor explicitly call the superclass constructor.
Connected to the case:
class C extends A.B {
C () {
new A (). super (); / / This is an internal class to realize the constructor call.
}
}
Construction method can also be written like this:
C (A a) {
a.super ();
} / / Use this constructor to create objects, to be written as C c = new C (a); a is A's object.
11, the exception class
JAVA, in addition to RunTimeException class, or throw other exceptions must be caught.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: