Java SE Lesson 2
1. 多态:父类型的引用可以指向子类型的对象。
public class PolyTest
{
public static void main(String[] args)
{
//Parent parent = new Parent();
//parent.sing();
//Child child = new Child();
//child.sing();
Parent p = new Child();
p.sing();
}
}
class Parent
{
public void sing()
{
System.out.println("parent is singing");
}
}
class Child extends Parent
{
public void sing()
{
System.out.println("child is singing");
}
}
2. Parent p = new Child();当使用多态方式调用方法时,首先检查父类中是否有sing()方法,如果没有则编译错误;如果有,再去调用子类的sing()方法。
3. 一共有两种类型的强制类型转换:
a) 向上类型转换(upcast):比如说将Cat类型转换为Animal类型,即将子类型转换为父类型。对于向上类型转换,不需要显式指定。
b) 向下类型转换(downcast):比如将Animal类型转换为Cat类型。即将父类型
转换为子类型。对于向下类型转换,必须要显式指定(必须要使用强制类型
转换)。
public class PolyTest2
{
public static void main(String[] args)
{
/*
Animal animal = new Cat();
Animal animal2 = new Animal();
animal2 = animal;
animal2.sing();
*/
/*
Animal animal = new Cat();
Animal animal2 = new Animal();
animal = animal2;
animal.sing();
*/
/*
Cat cat = new Cat();
Animal animal = cat;
animal.sing();
*/
/*
Animal animal = new Animal();
Cat cat = (Cat)animal;//编译通过,执行错误
*/
//向上类型转换
Cat cat = new Cat();
Animal animal = cat;
animal.sing();
//向下类型转换
Animal a = new Cat();
Cat c = (Cat)a;
c.sing();
}
}
class Animal
{
public void sing()
{
System.out.println("animal is singing");
}
}
class Dog extends Animal
{
public void sing()
{
System.out.println("dog is singing");
}
}
class Cat extends Animal
{
public void sing()
{
System.out.println("cat is singing");
}
}
--------------------------------------------------------------------------------------------------------------------------------------------
public class PolyTest3
{
public static void main(String[] args)
{
//Fruit f = new Pear();
//f.run();
//Pear p = (Pear)f;
//p.run();
//Fruit f = new Pear();
//f.grow();
Fruit f = new Pear();
Pear p = (Pear)f;
p.grow();
}
}
class Fruit
{
public void run()
{
System.out.println("fruit is running");
}
}
class Pear extends Fruit
{
public void run()
{
System.out.println("pear is running");
}
public void grow()
{
System.out.println("pear is growing");
}
}
-------------------------------------------------------------------------------------------------------
public class PolyTest4
{
public static void main(String[] args)
{
A a = null;
if(args[0].equals("1"))
{
a = new B();
}
else if(args[0].equals("2"))
{
a = new C();
}
else if(args[0].equals("3"))
{
a = new D();
}
a.method();
}
}
class A
{
public void method()
{
System.out.println("A");
}
}
class B extends A
{
public void method()
{
System.out.println("B");
}
}
class C extends A
{
public void method()
{
System.out.println("C");
}
}
class D extends A
{
public void method()
{
System.out.println("D");
}
}
--------------------------------------------------------------------------------------------------------------------------
public class PolyTest5
{
/*
public void run(BMW bmw)
{
bmw.run();
}
public void run(QQ qq)
{
qq.run();
}
*/
public void run(Car car)
{
car.run();
}
public static void main(String[] args)
{
/*
PolyTest5 test = new PolyTest5();
BMW bmw = new BMW();
test.run(bmw);
QQ qq = new QQ();
test.run(qq);
*/
PolyTest5 test = new PolyTest5();
Car car = new BMW();
test.run(car);
QQ qq = new QQ();
test.run(qq);
}
}
class Car
{
public void run()
{
System.out.println("car is running");
}
}
class BMW extends Car
{
public void run()
{
System.out.println("BMW is running");
}
}
class QQ extends Car
{
public void run()
{
System.out.println("QQ is running");
}
}
4. 抽象类(abstract class):使用了abstract关键字所修饰的类叫做抽象类。抽
象类无法实例化,也就是说,不能new出来一个抽象类的对象(实例)。
public abstract class AbstractTest
{
}
5. 抽象方法(abstract method):使用abstract关键字所修饰的方法叫做抽象方
法。抽象方法需要定义在抽象类中。相对于抽象方法,之前所定义的方法叫
做具体方法(有声明,有实现)。
public class Test
{
public static void main(String[] args)
{
//R r = new R();//抽
象类无法实例化,也就是说,不能new出来一个抽象类的对象(实例)。
}
}
abstract class T
{
public abstract void method();//抽象方法需要定义在抽象类中
public void test()//如果某个类是抽象类,那么该类可以包含具体方法(有声明、有实现)
{
System.out.println("test");
}
}
abstract class R extends T
{
public void method()//在子类继承父类(父类是个抽象类)的情况下,那么该子类必须要实现父类
{
System.out.println("method");
}
}
6. 如果一个类包含了抽象方法,那么这个类一定是抽象类。
7. 如果某个类是抽象类,那么该类可以包含具体方法(有声明、有实现)。
8. 如果一个类中包含了抽象方法,那么这个类一定要声明成abstract class,也
就是说,该类一定是抽象类;反之,如果某个类是抽象类,那么该类既可以
包含抽象方法,也可以包含具体方法。
9. 无论何种情况,只要一个类是抽象类,那么这个类就无法实例化。
10. 在子类继承父类(父类是个抽象类)的情况下,那么该子类必须要实现父类
中所定义的所有抽象方法;否则,该子类需要声明成一个abstract class。
public class Test2
{
public static void main(String[] args)
{
Shape shape = new Triangle(10,6);
int area = shape.computeArea();
System.out.println("triangle:" + area);
shape = new Rectangle(10, 10);
area = shape.computeArea();
System.out.println("rectangle:" + area);
}
}
abstract class Shape
{
public abstract int computeArea();//计算形状面积
}
class Triangle extends Shape
{
int width;
int height;
public Triangle(int width, int height)
{
this.width = width;
this.height = height;
}
public int computeArea()
{
return (width * height) / 2;
}
}
class Rectangle extends Shape
{
int width;
int height;
public Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
public int computeArea()
{
return this.width * this.height;
}
}
11. 接口(interface):接口的地位等同于class,接口中的所有方法都是抽象方
法。在声明接口中的方法时,可以使用abstract关键字,也可以不使用。通
常情况下,都会省略掉abstract关键字。
public interface InterfaceTest
{
public void output();
}
12. 可以将接口看作是特殊的抽象类(抽象类中可以有具体方法,也可以有抽象
方法,而接口中只能有抽象方法,不能有具体方法)。
13. 类可以实现接口。实现使用关键字implements表示,代表了某个类实现了
某个接口。
public class Test3
{
public static void main(String[] args)
{
MyClass myClass = new MyClass();
myClass.output();
myClass.output2();
myClass.output3();
}
}
interface MyInterface
{
public void output();
}
interface MyInterface2
{
public void output2();
}
class MyParent
{
public void output3()
{
System.out.println("output3");
}
}
class MyClass extends MyParent implements MyInterface, MyInterface2
{
public void output()//一个类实现了某个接口,那么该类必须要实现接口中声明的所有方法。
{
System.out.println("output");
}
public void output2()
{
System.out.println("output2");
}
}
14. 一个类实现了某个接口,那么该类必须要实现接口中声明的所有方法。如果
该类是个抽象类,那么就无需实现接口中的方法了。
15. Java是单继承的,也就是说某个类只能有唯一一个父类;一个类可以实现多
个接口,多个接口之间使用逗号分隔。
16. 多态:所谓多态,就是父类型的引用可以指向子类型的对象,或者接口类型的引用可以指向实现该接口的类的实例。关于接口与实现接口的类之间的强
制类型转换方式与父类和子类之间的强制类型转换方式完全一样。
public class Test4
{
public static void main(String[] args)
{
/*
BB bb = new BB();
AA aa = bb;
bb.output();
*/
AA aa = new BB();
BB bb = (BB)aa;
bb.output();
}
}
interface AA
{
public void output();
}
class BB implements AA
{
public void output()
{
System.out.println("BB");
}
}
17. static关键字:可以用于修饰属性,也可以用于修饰方法,还可以用于修饰
类(后面的课程讲)
public class StaticTest
{
public static void main(String[] args)
{
/*
MyStatic myStatic = new MyStatic();
MyStatic myStatic2 = new MyStatic();
myStatic.a = 10;
System.out.println(myStatic2.a);
*/
MyStatic myStatic = new MyStatic();
MyStatic.a = 10;
System.out.println(myStatic.a);
}
}
class MyStatic
{
static int a;
}
18. static修饰属性:无论一个类生成了多少个对象,所有这些对象共同使用唯
一一份静态的成员变量;一个对象对该静态成员变量进行了修改,其他对象
的该静态成员变量的值也会随之发生变化。如果一个成员变量是static的,
那么我们可以通过类名.成员变量名的方式来使用它(推荐使用这种方式)。
19. static修饰方法:static修饰的方法叫做静态方法。对于静态方法来说,可以
使用类名.方法名的方式来访问。
public class StaticTest2
{
public static void main(String[] args)
{
//MyStatic2 test = new MyStatic2();
//test.output();
MyStatic2.output();
}
}
class MyStatic2
{
public static void output()
{
System.out.println("output");
}
}
20.静态方法只能继承,不能重写(Override)。
public class StaticTest3
{
public static void main(String[] args)
{
M m = new N();
m.output();
}
}
class M
{
public void output()
{
System.out.println("M");
}
}
class N extends M
{
public static void output()
{
System.out.println("N");
}
}
21. final关键字:final可以修饰属性、方法、类。
22. final修饰类:当一个类被final所修饰时,表示该类是一个终态类,即不能被
继承。
public class FinalTest
{
public static void main(String[] args)
{
F f = new F();
}
}
final class E // 该类不能被继承
{
}
class F extends E
{
}
23. final修饰方法:当一个方法被final所修饰时,表示该方法是一个终态方法,
即不能被重写(Override)。
public class FinalTest2
{
public static void main(String[] args)
{
H h = new H();
h.output();
}
}
class G
{
public final void output()
{
System.out.println("G");
}
}
class H extends G
{
public void output()
{
System.out.println("H");
}
}
24. final修饰属性:当一个属性被final所修饰时,表示该属性不能被改写。
public class FinalTest3
{
public static void main(String[] args)
{
People people = new People();
//people.age = 20;
//people.address = new Address();
people.address.name = "shanghai";
}
}
class People
{
//final int age = 10;
final Address address = new Address();
}
class Address
{
String name = "beijing";
}
25. 当final修饰一个原生数据类型时,表示该原生数据类型的值不能发生变化
(比如说不能从10变为20);如果final修饰一个引用类型时,表示该引用
类型不能再指向其他对象了,但该引用所指向的对象的内容是可以发生变化
的。
26. 对于final类型成员变量,一般来说有两种赋初值方式:
a) 在声明final类型的成员变量时就赋上初值
b) 在声明final类型的成员变量时不赋初值,但在类的所有构造方法中都为其赋
上初值。
public class FinalTest4
{
final int a;
public FinalTest4()
{
a = 0;
}
public FinalTest4(int a)
{
this.a = a;
}
}
27. static代码块:静态代码块。静态代码块的作用也是完成一些初始化工作。首
先执行静态代码块,然后执行构造方法。静态代码块在类被加载的时候执行,
而构造方法是在生成对象的时候执行;要想调用某个类来生成对象,首先需
要将类加载到Java虚拟机上(JVM),然后由JVM加载这个类来生成对象。
public class StaticTest4
{
public static void main(String[] args)
{
new S();
new S();
}
}
class P
{
static
{
System.out.println("P static block");
}
public P()
{
System.out.println("P constructor");
}
}
class Q extends P
{
static
{
System.out.println("Q static block");
}
public Q()
{
System.out.println("Q constructor");
}
}
class S extends Q
{
static
{
System.out.println("S static block");
}
public S()
{
System.out.println("S constructor");
}
}
28. 类的静态代码块只会执行一次,是在类被加载的时候执行的,因为每个类只
会被加载一次,所以静态代码块也只会被执行一次;而构造方法则不然,每
次生成一个对象的时候都会调用类的构造方法,所以new一次就会调用构造
方法一次。
29. 如果继承体系中既有构造方法,又有静态代码块,那么首先执行最顶层的类
的静态代码块,一直执行到最底层类的静态代码块,然后再去执行最顶层类
的构造方法,一直执行到最底层类的构造方法。注意:静态代码块只会执行
一次。
30. 不能在静态方法中访问非静态成员变量;可以在静态方法中访问静态的成员
变量。可以在非静态方法中访问静态的成员变量。
public class StaticTest5
{
public static void main(String[] args)
{
W w = new W();
w.change();
}
}
class W
{
static int a = 10;
public static void change()
{
a++;
}
}
31. 总结:静态的只能访问静态的;非静态的可以访问一切。
32. 不能在静态方法中使用this关键字。