1. 面向对象,在思考时,需要将待处理的事物分类,将数据
和对数据的操作进行封装,作为一个整体。
例子:
1 public class Car 2 { 3 private String carName; 4 private Date madeDate; 5 private String color; 6 7 public void drive() 8 { 9 10 } 11 12 public static void main(String[] args) 13 { 14 Car c = new Car(); 15 c.drive(); 16 } 17 }
2. 构造函数
在创建类的对象时,有时候需要一些必要的操作,而且若没有进行,后果会很严重。这部分操作可以放在构造函数中,构造函数会在创建对象时被自动调用。
1 public class Car 2 { 3 private String carName; 4 private String color; 5 6 Car() 7 { 8 carName = "newcar"; 9 color = "red"; 10 } 11 public void drive() 12 { 13 14 } 15 16 public static void main(String[] args) 17 { 18 Car c = new Car(); 19 c.drive(); 20 } 21 }
3. 构造函数和成员函数类似,可以被才重载
1 public class Car 2 { 3 private String carName; 4 private String color; 5 6 Car() 7 { 8 carName = "newcar"; 9 color = "red"; 10 } 11 12 Car(String carName, String color) 13 { 14 this.carName = carName; 15 this.color = color; 16 } 17 public void drive() 18 { 19 } 20 21 public static void main(String[] args) 22 { 23 Car c = new Car(); 24 c.drive(); 25 } 26 }
程序运行时,编译器会根据函数的参数个数,类型来选择执行哪个函数。
this是一个特殊的对象,指的是调用这个函数的那个对象。
4. static成员变量,成员函数
static变量,也叫做类变量,是整个类的变量,只有一份,所有该类的对象共享。
static函数,函数不管是否是static,都只有一份代码。static函数可以直接通过类来调用。
1 import java.util.Date; 2 3 4 public class Car 5 { 6 private String carName; 7 private String color; 8 static int carNumber = 0; 9 10 Car() 11 { 12 carName = "newcar"; 13 color = "red"; 14 carNumber++; 15 } 16 17 Car(String carName, String color) 18 { 19 this.carName = carName; 20 this.color = color; 21 } 22 public void drive() 23 { 24 } 25 26 public int getCarNum() 27 { 28 return carNumber; 29 } 30 31 public static void main(String[] args) 32 { 33 Car c = new Car(); 34 c.drive(); 35 } 36 }
静态方法不能访问非静态方法和变量
非静态方法可以访问静态方法和变量
5 static块和初始化顺序
1 public class TestStatic 2 { 3 private static int i = 1; 4 5 static 6 { 7 i = 10; 8 } 9 public static void main(String[] args) 10 { 11 System.out.println(i); 12 } 13 }
结果为10
1 public class TestStatic 2 { 3 static 4 { 5 i = 10; 6 } 7 private static int i = 1; 8 9 public static void main(String[] args) 10 { 11 System.out.println(i); 12 } 13 }
结果为1
6 常量
private static final double PI = 3.14;
java中可以通过final关键字指定变量为常量,一旦被初始化赋值,不能再被修改。
且常量被定义时,若不是static ,可以定义时初始化。或者,在构造函数中初始化。
当是static,则一定要在定义时初始化。
而且,一般来说,常量在类中都作为static变量存在,节省空间。
7 继承
java中允许类的继承,一方面是现实生活中,不同的类之间确实存在这样的关系,第二
是继承可以实现重用,可以从已有的资源中简单的实现我们需要的功能。
1 package animal.com.wyq; 2 3 public class Animal 4 { 5 private String name; 6 public void sleep() 7 { 8 System.out.println(" animal sellp"); 9 } 10 11 public static void main(String[] args) 12 { 13 Animal an = new Animal(); 14 an.sleep(); 15 } 16 }
输出:
1 package animal.com.wyq; 2 3 public class Cat extends Animal 4 { 5 6 public static void main(String[] args) 7 { 8 Cat cat = new Cat(); 9 cat.sleep(); 10 } 11 }
输出:
可以看到,Cat类直接继承了Animal类,直接获得了父类的成员变量和成员函数。
当然,子类可能有和父类相同的方法,也就是接口,但是有不同的实现,因此子类可以
重写父类的方法:
1 package animal.com.wyq; 2 3 public class Cat extends Animal 4 { 5 public void sleep() 6 { 7 System.out.println(" cat sellp"); 8 } 9 public static void main(String[] args) 10 { 11 Cat cat = new Cat(); 12 cat.sleep(); 13 } 14 }
输出:
在父类和子类之间,子类重新实现父类方法,override,覆盖,重写。
8 继承的构造函数
当构造子类对象时,系统会先调用父类的构造函数,默认是调用不带参数的父类构造函数,在调用子类的构造函数。
当父类没有无参数构造函数时,需要在子类构造函数中,显式得到利用super调用父类的带参构造函数。
1 package animal.com.wyq; 2 3 class Animal 4 { 5 private String name; 6 7 Animal(String name) 8 { 9 this.name = name; 10 System.out.println("animal cons"); 11 } 12 } 13 14 public class Cat extends Animal 15 { 16 Cat() 17 { 18 super("miao1"); 19 System.out.println("cat cons"); 20 } 21 22 public static void main(String[] args) 23 { 24 Cat cat = new Cat(); 25 } 26 }
父类的构造方法不会被子类继承,所以在子类构造函数中,不能写Animal("miao");
只能用super()关键字。
9 多态
当存在类之间的继承关系,而且子类重写了父类的方法,最后,当存在父类引用指向子类对象时,父类调用自己的方法,
实际上调用的是子类重写之后的具体实现。
1 package animal.com.wyq; 2 3 class Animal 4 { 5 public void sleep() 6 { 7 System.out.println("animal sleep"); 8 } 9 } 10 11 class Cat extends Animal 12 { 13 public void sleep() 14 { 15 System.out.println("Cat sleep"); 16 } 17 } 18 19 class Dog extends Animal 20 { 21 public void sleep() 22 { 23 System.out.println("Dog sleep"); 24 } 25 } 26 27 28 public class Test 29 { 30 public static void TestSleep(Animal an) 31 { 32 an.sleep(); 33 } 34 35 public static void main(String[] args) 36 { 37 Animal an = new Cat(); 38 Test.TestSleep(an); 39 40 an = new Dog(); 41 Test.TestSleep(an); 42 43 } 44 }
输出
这样,可以清楚的看到多态的好处,具体的函数的参数使用父类引用,当我们实际中传入的是某个具体的子类
则在调用方法时,就会调用对应的子类的具体实现。
instanceof操作符:判断一个对象是否是某个类的对象
1 package animal1.com.wyq; 2 3 class Animal 4 { 5 } 6 7 class Cat extends Animal 8 { 9 } 10 11 public class Test1 12 { 13 public static void main(String[] args) 14 { 15 Cat cat = new Cat(); 16 17 boolean bool = (cat instanceof Cat); 18 System.out.println(bool); 19 20 bool = (cat instanceof Animal); 21 System.out.println(bool); 22 } 23 }
输出,两true