public class TestA {
public static void main(String[] args) {
new D().print();;// 子类实例化时,必须先实例化父类,再实例化子类
}
}
class A {
A() {
System.out.println("A");
}
}
class B extends A {
B() {
System.out.println("B");
}
}
class C {
int a = 1;
C() {
System.out.println("----" + a);
a = 2;
}
}
class D extends C { //new D().print();
D() {
System.out.println("+++++" + a);
a = 3;
}
void print() {
System.out.println("====" + a);
}
}
class Animal {
int p = 1;
public void move() {
System.out.println("动物可以移动");
}
public void a() {
System.out.println();
}
}
class Dog extends Animal {
int p = 2;
public void move() {
System.out.println("狗可以跑");
}
public void b() {
System.out.println();
}
@Override
public String toString() {
return "aaaaaaaaaaaaaa";
}
}
@Test
public void test2() {
Dog dog = new Dog();
dog.a();
dog.b();
dog.move();
System.out.println(dog.p);
}
输出结果:
@Test
public void test3() {
Animal a1 = new Dog();// (自动)向上转型
a1.a();
a1.move();
System.out.println(a1.p);
Object d = new Dog();
System.out.println(d.toString());
ArrayList list = new ArrayList<>();
List list1 = new ArrayList<>();
// 面向接口的变成方式
}
@Test
public void test4() {
Animal a = new Animal();
Dog b = new Dog();
Animal c = new Dog();
Object d = new Dog();
System.out.println(b instanceof Dog);//true
System.out.println(b instanceof Animal);//true
System.out.println(d instanceof Dog);//true
System.out.println(a instanceof Dog);//false
//instanceof左侧变量的对象,能否给右侧类型的变量赋值
System.out.println(c instanceof Dog);//true
System.out.println(a.p);//1
System.out.println(b.p);//2
System.out.println(c.p);//1
//System.out.println(d.p);//错误
}
}
输出结果:
向上转型例题:
向下转型
@Test
public void test6() {
Animal c = new Dog();// (自动)向上转型
int t0 = 100;
long t1 = t0;
int t2 = (int) t1;
Dog e = (Dog) c;// (强制)向下转型
// e.p;
// e.b();
// e.a();
e.move();// 狗可以跑
}
输出结果:
class Temp {
Object test() {
return null;
}
}
package demo211201;
this、super、final、static关键字
this关键字
public class TestF {
// this:当前对象的引用,用来访问当前对象中的成员
// this代指=调用(this所在的方法)的对象
public void setTestThis(int testThis) {
this.testThis = testThis;
}
private int testThis = 100;
public int getTestThis() {
return testThis;
}
public static void main(String[] args) {
TestF t1 = new TestF();
t1.setTestThis(200);
System.out.println(t1.getTestThis());
TestF t2 = new TestF();
t2.setTestThis(300);
System.out.println(t2.getTestThis());
TestF t3 = new TestF(500);
System.out.println(t3.getTestThis());
TestF t4 = new TestF();
System.out.println(t4.getTestThis());
}
public TestF() {
// this(400);
}
public TestF(int testThis) {
this();
this.testThis = testThis;
System.out.println(testThis);
}
}
this关键字
package demo211201;
super关键字
import org.junit.Test;
public class TestG extends TestGSuper {
private int id = 0;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public TestG(int id) {
super(111000);//父类有参必须写且赋值
//super();//调用父类无参构造方法
this.id = id;
}
// super:上级对象的引用+用来访问上级对象中的成员
// super代指=调用super所在的方法的对象在实例化时先实例化父类的对象
public static void main(String[] args) {
TestG t = new TestG(1000);
t.temp();
}
public void temp() {
System.out.println(getText1());
System.out.println(this.getText1());
System.out.println(super.getText1());
}
@Override
public int getText1() {
return 10;
}
}
class TestGSuper {
public TestGSuper(int test1) {
super();
this.test1 = test1;
}
private int test1;
public int getText1() {
return test1;
}
public void setText1(int text1) {
this.test1 = text1;
}
}
final关键字
package demo211201;
static关键字
import org.junit.Test;
public class TestJ {
public static void main(String[] args) {
// TestJ.a = 101;// 标准调用格式
// f1();// 省略调用格式
}
void test() {
TestJ t = new TestJ();
t.b = 201;// 标准调用格式
t.f2();// 省略调用格式
System.out.println(b);
System.out.println(this.b);
}
// static 功能修饰符
static int a = 100;
int b = 200;
static void f1() {
}
void f2() {
}
@Test
public void test3() {
TestJ t1 = new TestJ();
TestJ t2 = new TestJ();
System.out.println(t1.b);
System.out.println(t2.b);
t1.b = 102;
t2.b = 202;
System.out.println(t1.b);
System.out.println(t2.b);
// 同个类的不同对象中的实例变量互不受影响
System.out.println(t1.a);
System.out.println(t2.a);
System.out.println(TestJ.a);
t1.a = 303;
t2.a = 404;
System.out.println(t1.a);
System.out.println(t2.a);
System.out.println(TestJ.a);
// 两个类的不同对象中的静态变量公用的是同一个
}
}
class E {
private static void testMethod() {
System.out.println("testMethod");
}
public static void main(String[] args) {
E e1 = (E) null;// 没对象
e1.testMethod();// testMethod
((E) null).testMethod();// testMethod
// e1.test();//空指针异常
E e2 = null;
e2.testMethod();// testMethod
}
private void test() {
System.out.println("test");
}
}
package demo211202;
public class TestB {
public static void main(String[] args) {
TestB1 t1= new TestB1();
t1.print();//haha
TestB2 t2= new TestB2();
t2.print();//hoho
TestB1 t3= new TestB2();
t3.print();//haha
}
}