访问修饰符
private: | 默认 | public | protected |
---|---|---|---|
变量、方法 | 类、变量、方法 | 类、变量、方法 | 变量、方法 |
本类 | 同一个包 | 任何地方 | 同一个包,不同包(继承关系) |
变量的使用,从小往大的方向找,大的必须包含小的。
——————————————————————————————
方法重写
继承关系: 父类和子类的方法名相同,参数列表相同(个数、顺序、类型都相同),方法的返回类型相同(如果是继承关系,子类的 方法的类型 要小于等于<=父类的)
子类方法的访问修饰符一定要 >=(大于等于) 父类
异常: 子类方法的异常(非运行时异常)<=(小于等于) 父类
为什么进行重写
扩展父类的方法的功能
私有方法不能被重写
继承关系中构造方法的调用规则
1子类创建对象时,总是默认调用父类无参的构造方法
2如果父类没有无参的构造方法,使用super()调用指定的构造方法
class A{ int x; int y;
publilc A(int x,int y){ …(“A”); } }
class B extends A{
public B(int x,int y){
super.(x,y); …(“B”); }
} B b =new B(1,2);
…为system.out.println省略
构造方法练习
class MyClass{
int value;
}
public class TestMyClass{
public static void main(String args[]){
MyClass mc1 = new MyClass();
MyClass mc2 = new MyClass(10);
System.out.println(mc1.value);
System.out.println(mc2.value);
}
}
构造方法的调用
class Super{
public Super(){
System.out.println("Super()");
} Super() Sub()
public Super(String str){ Super() Sub() Sub(int)
System.out.println("Super(String)"); Super(String) Sub(String)
}
}
class Sub extends Super{
public Sub(){
System.out.println("Sub()");
}
public Sub(int i){
this();
System.out.println("Sub(int)");
}
public Sub(String str){
super(str);
System.out.println("Sub(String)");
}
}
public class TestSuperSub{
public static void main(String args[]){
Sub s1 = new Sub();
Sub s2 = new Sub(10);
Sub s3 = new Sub("hello");
}
}
多态:
定义: 一种类型的变量可以指向不同的对象
Animal a1 = new Bird();
Animal a2 = new Dog();
编译时的多态: 方法的重载
class A {
void f(){
。。。("f");
}
void f(int x){
。。。(“f”+x);
}
}
A a1 =new A ();
A a2 =new A();
运行时的多态: 方法的重写
Animal a1 = new Bird();
Animal a2 = new Dog();
a1.eat();
a2.eat();
在编译时,看变量的声明类型
运行时,如果子类重写了父类的方法,他会动态绑定到子类上,运行子类的方法。(没有动态绑定变量)
只有子类重写了父类方法的时候,才会产生动态绑定的情况
多态练习:
class Super{
public void method(){
System.out.println("method() in Super");
}
public void method(int i){
System.out.println("method(int) in Super");
}
}
class Sub extends Super{
public void method(){
System.out.println("method() in Sub");
}
public void method(String str){
System.out.println("method(String) in Sub");
}
}
public class TestSuperSub{
public static void main(String args[]){
Super s = new Sub();
s.method(10);
s.method();
s.method("hello");//编译错误
}
}
多态的应用:
方法的参数定义为父类类型或者接口类型
对象造型(对象强制转换)
instanceof,用于判断传入的数值为什么类型的,然后进行相应类型的对应
//通过instanceof判断
public void test(Animal animal){
if(animal instanceof Dog){
((Dog) animal).d();
}if(animal instanceof Bird){
((Bird) animal).b();
}
}
static:修饰变量,方法,静态块
变量:
成员变量:类变量(静态变量):由static修饰的成员变量;实例变量:没有static修饰
局部变量:不可以被static修饰
类变量和实例变量的区别:
类变量是对所有对象共享的(公用的),而实例对象是归对象所有的,不是公用的。
静态变量可以由类名调用
静态变量:类加载器加载类的时候初始化的
变量的初始化过程:
static int x = 5;
int x = 6;//一个对象一个实例变量,申请内存,赋值默认值
修饰方法:
静态方法:由static修饰的
实例方法:没有static修饰,需要new 对象,
int x 1;
static int y = 2;
void f(){
}
static void f2(){
}
void test(){
x = 2;
y = 3;
f();
f2();
}
void test2(){
x = 2; // error
y = 3;
f(); //error,静态成员的初始化要先于实例成员,方法需要先创建对象,非静态的可以直接使用静态的,静态的不可以直接使用非静态的
f2();
}
静态方法是否可以重写?重载?继承?
静态方法不能叫做重写,它和对象无关,所以不能重写
可以进行重载
可以被继承
构造方法是否可以定义静态的?不可以,构造方法是创造对象使用的
静态方法是否可以使用this,super?不可以
静态成员要优于对象,this不能使用
动态绑定,父类声明,子类调用,最终出现的是子类的结果。
静态块:
static {}
写到类体里面,在类加载时,执行一次
public class Hello{
}
静态块和构造块
父类的静态块——>子类的静态块——>父类的构造块——>父类的构造方法——>子类的构造块——>子类的构造方法
练习:
class A{
static D d;
static {…(“A1”); d = new D();}
{…(“A2”);}
public A(){…(“A3”);}
}
class B extends A{
static C c = new C();
static {.....("B1");}
{.........("B2");}
public B(){.........("B3");}}
class C{
public C(){........("C");}
}
class D extends C{
public D(){........("D");}
}
new B(); A1 C D C B1 A2 A3 B2 B3
final
final:可以修饰类,方法,变量
变量:常量:
常量名:一般名字的所有字母都大写,如果有多个单词组成,单词之间用下划线
格式:final int PRICE =10;
特点:不能改
final修饰的方法:不能重写,保证了结果的唯一性
final修饰类不能被继承
练习:
class Super{
public final void m1(){
System.out.println("m1() in Super");
}
public void m1(int i){
System.out.println("m1(int) in Super");
}
}
class Sub extends Super{
public void m1(int i){
System.out.println("m1(int) in Sub");
}
public void m1(double d){
System.out.println("m1(double) in Sub");
}
}
public class Test {
public static void main(String args[]){
Sub s = new Sub();
s.m1();
s.m1(10);
s.m1(1.5);
}
}
final练习
package cn.tedu.demo.final1;
public class Demo {
public static final int PRICE =100;
final int PRICE2;
static final int PRICE3;
static{
PRICE3=5;
}
public Demo(){
PRICE2 =3;
}
public Demo(int x){
//PRICE2 =3;//必须每个构造方法都需要有值
this();
}
void f(){//方法内是否可以定义常量,static不可以,final可以
//static int x=9;
final int x=9;
}
final void test(){
}
class Demo2 extends Demo{
}
}