1. class TestA { 2. public void start() { System.out.println("TestA"); } 3. } 4. public class TestB extends TestA { 5. public void start() { System.out.println("TestB"); } 6. public static void main(String[] args) { 7. ((TestA)new TestB()).start(); 8. } 9. } What is the result? A. TestA B. TestB C. Compilation fails. D. An exception is thrown at runtime. Answer: B那为什么这个强转之后还是B呢
1. class TestA { 2. public void start() { System.out.println("TestA"); } 3. } 4. public class TestB extends TestA { 5. public void start() { System.out.println("TestB"); } 6. public static void main(String[] args) { 7. ((TestA)new TestB()).start(); 8. } 9. } What is the result? A. TestA B. TestB C. Compilation fails. D. An exception is thrown at runtime. Answer: B那为什么这个强转之后还是B呢 这需要强转吗?这不就是向上转型吗?
1. class TestA { 2. public void start() { System.out.println("TestA"); } 3. } 4. public class TestB extends TestA { 5. public void start() { System.out.println("TestB"); } 6. public static void main(String[] args) { 7. ((TestA)new TestB()).start(); 8. } 9. } What is the result? A. TestA B. TestB C. Compilation fails. D. An exception is thrown at runtime. Answer: B那为什么这个强转之后还是B呢 这需要强转吗?这不就是向上转型吗?呃,那上面那个为什么是强转呢
1. class TestA { 2. public void start() { System.out.println("TestA"); } 3. } 4. public class TestB extends TestA { 5. public void start() { System.out.println("TestB"); } 6. public static void main(String[] args) { 7. ((TestA)new TestB()).start(); 8. } 9. } What is the result? A. TestA B. TestB C. Compilation fails. D. An exception is thrown at runtime. Answer: B那为什么这个强转之后还是B呢 这需要强转吗?这不就是向上转型吗? 哥,类名是不能调用非静态方法的!!!应该就是这个问题。
1. class TestA { 2. public void start() { System.out.println("TestA"); } 3. } 4. public class TestB extends TestA { 5. public void start() { System.out.println("TestB"); } 6. public static void main(String[] args) { 7. ((TestA)new TestB()).start(); 8. } 9. } What is the result? A. TestA B. TestB C. Compilation fails. D. An exception is thrown at runtime. Answer: B那为什么这个强转之后还是B呢 这需要强转吗?这不就是向上转型吗? 哥,类名是不能调用非静态方法的!!!应该就是这个问题。它new了可以调用吧
Animal a = new Dog(); 调用成员变量看左边, 调用成员属性,先看左边有没有该方法,有则调用右边的该方法,因为该方法被覆盖了
完美解决!Java中的许多对象(一般都是具有父子类关系的父类对象)在运行时都会出现两种类型:编译时类型和运行时类型,例如:Person person = new Student();这行代码将会生成一个person变量,该变量的编译时类型是Person,运行时类型是Student。 说明一下编译时类型和运行时类型: Java的引用变量有两个类型,一个是编译时类型,一个是运行时类型,编译时类型由声明该变量时使用的类型决定,运行时类型由实际赋给该变量的对象决定。如果编译时类型和运行时类型不一致,会出现所谓的多态。因为子类其实是一种特殊的父类,因此java允许把一个子类对象直接赋值给一个父类引用变量,无须任何类型转换,或者被称为向上转型,由系统自动完成。 引用变量在编译阶段只能调用其编译时类型所具有的方法,但运行时则执行它运行时类型所具有的方法,因此,编写Java代码时,引用变量只能调用声明该变量所用类里包含的方法。与方法不同的是,对象的属性则不具备多态性。通过引用变量来访问其包含的实例属性时,系统总是试图访问它编译时类所定义的属性,而不是它运行时所定义的属性。
这相当于 Base s = new Sub();是向上造型!!当变量与子类相同时,调的是父类变量,当方法与子类相同时(重写),则调用子类的方法!!!
1. class TestA {
2. public void start() { System.out.println("TestA"); }
3. }
4. public class TestB extends TestA {
5. public void start() { System.out.println("TestB"); }
6. public static void main(String[] args) {
7. ((TestA)new TestB()).start();
8. }
9. }
What is the result?
A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: B那为什么这个强转之后还是B呢
1. class TestA {
2. public void start() { System.out.println("TestA"); }
3. }
4. public class TestB extends TestA {
5. public void start() { System.out.println("TestB"); }
6. public static void main(String[] args) {
7. ((TestA)new TestB()).start();
8. }
9. }
What is the result?
A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: B那为什么这个强转之后还是B呢
这需要强转吗?这不就是向上转型吗?
1. class TestA {
2. public void start() { System.out.println("TestA"); }
3. }
4. public class TestB extends TestA {
5. public void start() { System.out.println("TestB"); }
6. public static void main(String[] args) {
7. ((TestA)new TestB()).start();
8. }
9. }
What is the result?
A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: B那为什么这个强转之后还是B呢
这需要强转吗?这不就是向上转型吗?呃,那上面那个为什么是强转呢
1. class TestA {
2. public void start() { System.out.println("TestA"); }
3. }
4. public class TestB extends TestA {
5. public void start() { System.out.println("TestB"); }
6. public static void main(String[] args) {
7. ((TestA)new TestB()).start();
8. }
9. }
What is the result?
A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: B那为什么这个强转之后还是B呢
这需要强转吗?这不就是向上转型吗?
哥,类名是不能调用非静态方法的!!!应该就是这个问题。
1. class TestA {
2. public void start() { System.out.println("TestA"); }
3. }
4. public class TestB extends TestA {
5. public void start() { System.out.println("TestB"); }
6. public static void main(String[] args) {
7. ((TestA)new TestB()).start();
8. }
9. }
What is the result?
A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: B那为什么这个强转之后还是B呢
这需要强转吗?这不就是向上转型吗?
哥,类名是不能调用非静态方法的!!!应该就是这个问题。它new了可以调用吧
调用成员变量看左边,
调用成员属性,先看左边有没有该方法,有则调用右边的该方法,因为该方法被覆盖了
(然后看优先级别)