一、类的名字
-
源文件名:源文件名必须和类名相同。当保存文件的时候,你应该使用类名作为文件名保存,文件名的后缀为
.java
。 -
类名:对于所有的类来说,类名的首字母应该大写(不大写也行,约定俗成)。(如果文件名和类名不相同则会导致编译错误)。
-
构造函数名:和类名一致。
-
方法名:所有的方法名都应该以小写字母开头(约定俗成)。如果方法名含有若干单词,则后面的每个单词首字母大写。
-
主方法入口:所有的Java 程序由
public static void main(String []args)
方法开始执行。
二、成员变量的定义
1.实例变量(非静态变量)
(1) 默认初始化为默认值
class Data {
long l; // Defaults to zero
}
方法中局部变量可不会默认初始化
void f() {
int i;
i++; // Error -- i not initialized
}
(2) 非静态初始化块
- 可以将初始化分开成定义和赋值两步。
- 块可以放在非静态变量后,也可以放在其前。
- 还可以插入函数调用。
- 可以和静态变量初始化块写在一个块中
class Tag {
Tag(int i)
{
System.out.println("Tag(" + i + ")");
}
void printHello()
{
System.out.println("hello");
}
}
public class Card {
Tag t1;
{
t1 = new Tag(1);
t2 = new Tag(2);
t1.printHello();
System.out.println("world");
}
Tag t2;
public static void main(String[] args) {
Card card = new Card();
}
}
2.类变量(静态变量)
(1)用static
修饰。
static int a;
static int b = 0;
(2)类变量的创建
只有当第一次被访问或者第一个类的对象被创建时才会被创建,且只会被创建一次。
class Tag {
Tag(int i)
{
System.out.println("Tag(" + i + ")");
}
}
public class Card {
static Tag t4 = new Tag(4);
public static void main(String[] args) {
Tag tag1 = Card.t4;
Tag tag2 = Card.t4;
}
}
只输出一个:
同理:都只创建一次。
Tag tag1 = Card.t4;
Card card = new Card();
Card card1 = new Card();
Card card2 = new Card();
(3)静态变量初始化块
- 可以将初始化分开成定义和赋值两步。
- 块可以放在静态变量后,也可以放在其前。
- 还可以插入函数调用。
- 可以和非静态变量初始化块写在一个块中
class Tag {
Tag(int i)
{
System.out.println("Tag(" + i + ")");
}
void printHello()
{
System.out.println("hello");
}
}
public class Card {
static Tag t1;
{
t1 = new Tag(1);
t2 = new Tag(2);
t1.printHello();
System.out.println("world");
}
static Tag t2;
public static void main(String[] args) {
Card card = new Card();
}
}
3.this指针
(1)Reference to Current Object
class Leaf{
Leaf increment() {
i++;
return this;
}
}
(2)Specifying a Member
class Flower {
String name;
Flower(String name) {
this.name = name;
}
}
(3)调用构造器
三、成员方法的定义
1.实例函数(非静态函数)
- 非
abstract
类的实例方法必须写函数体:public abstract void eat(){...}
- 只有
abstract
类的abstract
实例方法才不用写函数体:public abstract void eat();
2.构造函数
构造函数算是一种特殊的成员函数
3.类函数(静态函数)
用
static
修饰
static void hello()
/*如何调用*/
class Tag {
Tag(int i)
{
System.out.println("Tag(" + i + ")");
}
}
public class Card {
static void createTag() {
Tag t = new Tag(1);
}
public static void main(String[] args) {
// 通过类来调用
Card.createTag();
// 通过实例来调用
Card card = new Card();
card.createTag();
}
}
/*
Tag(1)
Tag(1)
*/
不能在静态函数中调用实例变量和实例函数,只能调用静态变量和静态函数,而且不能用
this
因为非静态函数还没有分配内存空间。
public class Card {
int a = 10;
static int b = 10;
void printA() {
System.out.println(this.a);
}
static void staticPrintA() {
// 不能访问实例变量
// System.out.println(a);
// 不能调用实例函数
// this.printA();
}
static void StaticPrintHello() {
System.out.println("hello");
}
static void staticPrintB() {
System.out.println(b);
// 不能用System.out.println(this.b);
// 可以调用静态方法
StaticPrintHello();
// 不能用this.StaticPrintHello();
}
public static void main(String[] args) {
Card.staticPrintB();
}
}
/*
10
hello
*/
四、对象的构造
1.如何构造
通过new
关键字
Class_name instance_name = new Class_name(arg[]);
2.构造中各元素的先后顺序
- 执行初始化的静态变量
PS:
类内的main:先执行初始化的静态变量,再执行main内的语句
类外的main:先执行main内的语句,再执行初始化的静态变量 - 执行真正赋值成员变量(不管在构造函数前还是后)
- 执行构造函数
(1)验证静态变量、成员变量、构造函数
/*类内的main*/
class Tag {
Tag(int i)
{
System.out.println("Tag(" + i + ")");
}
}
public class Card {
Tag t1 = new Tag(1); // Before constructor
Card() {
System.out.println("Card()");
Tag t33 = new Tag(33);
}
Tag t2 = new Tag(2); // After constructor
void f() {
System.out.println("f()");
}
Tag t3 = new Tag(3); // At end
static Tag t4 = new Tag(4);
public static void main(String[] args) {
System.out.println("main begin...");
Card c = new Card();
System.out.println("main end...");
}
}
/*
Tag(4)
main begin...
Tag(1)
Tag(2)
Tag(3)
Card()
Tag(33)
main end...
*/
/*类外的main*/
class Tag {
Tag(int i)
{
System.out.println("Tag(" + i + ")");
}
}
class Card {
Tag t1 = new Tag(1); // Before constructor
Card() {
System.out.println("Card()");
Tag t33 = new Tag(33);
}
Tag t2 = new Tag(2); // After constructor
void f() {
System.out.println("f()");
}
Tag t3 = new Tag(3); // At end
static Tag t4 = new Tag(4);
}
public class Demo {
public static void main(String[] args) {
System.out.println("main begin...");
new Card();
System.out.println("main end...");
}
}
/*
main begin...
Tag(4)
Tag(1)
Tag(2)
Tag(3)
Card()
Tag(33)
main end...
*/
(2)块
- 静态变量的执行顺序
- 静态变量初始化赋值会最先执行
- 但分开在块中赋值的静态变量等同于实例变量,按真正赋值顺序执行。
- 实例变量的执行顺序,只看有没有真正的赋值:
- t0定义了但没有赋值,就不执行
- t3赋值在t2初始化赋值前,就先执行t3。
public class Card {
Tag t0;
static Tag t6;
Tag t1 = new Tag(1); // Before block
{
t3 = new Tag(3);
t0 = new Tag(0);
t6 = new Tag(6);
t7 = new Tag(7);
}
Card() {
System.out.println("Card()");
Tag t33 = new Tag(33); // Reinitialize t3
}
Tag t2 = new Tag(2); // After block , before definition
Tag t3;
static Tag t7;
Tag t4 = new Tag(4); // After definiton
static Tag t5 = new Tag(5);
public static void main(String[] args) {
new Card();
}
}
/*
Tag(5)
Tag(1)
Tag(3)
Tag(0)
Tag(6)
Tag(7)
Tag(2)
Tag(4)
Card()
Tag(33)
*/
五、成员变量的访问
instance_name.variable_name
六、成员函数的访问
3.静态函数
- 用类调用
public class Hello{
public static void printHello()
{
System.out.println("hello");
}
public static void main(String[] args) {
Hello.printHello();
}
}
- 用实例化后的对象调用
public class Hello{
public static void printHello()
{
System.out.println("hello");
}
public static void main(String[] args) {
Hello h = new Hello();
h.printHello();
}
}
使用非实例化的对象会报错,即使调用静态函数也不行。
七、final关键字
1.变量
- 就是常量,相当于
constant
,不可修改。 - 定义方式
- 初始化:
final int i = 100;
- 声明(这种形式的变量叫做blank final):
final int i;
- 初始化:
- 在使用前必须给声明的final赋值,否则报错
class Poppet {
private int i;
Poppet(int ii) { i = ii; }
}
public class BlankFinal {
private final int i = 0; // Initialized final
private final int j; // Blank final
private final Poppet p; // Blank final reference
public BlankFinal() {
j = 1; // Initialize blank final
p = new Poppet(1); // Initialize blank final reference
}
public BlankFinal(int x) {
j = x; // Initialize blank final
p = new Poppet(x); // Initialize blank final reference
}
public static void main(String[] args) {
new BlankFinal();
new BlankFinal(47);
}
}
2.方法
方法就不能再被子类重写
3.类
类就不能再被继承