1 class Base
2 {
3 int a = 1;
4 void display()
5 {
6 System.out.println("base");
7 }
8 }
9
10 class Derive extends Base
11 {
12 int a = 2;
13 int b = 3;
14 void display()
15 {
16 System.out.println("derive");
17 }
18 void displayA()
19 {
20 System.out.println(a + " " + b);
21 }
22
23 }
24 public class Demo
25 {
26 public static void main(String args[])
27 {
28 // Base base = new Base();
29 // base.display();
30 // Derive derive = new Derive();
31 // derive.display();
32 // derive.displayA();
33 Base base;
34 Derive derive = new Derive();
35 base = derive;
36 base.display();
37 System.out.println(base.a);
38
39 }
40 }
运行结果为:
run:
derive
1
调用的是父类数据成员和数子类函数,对比c++:
1 #include <iostream>
2 class Base
3 {
4
5 public:
6 int a ;
7 Base(int arg)
8 {
9 a = arg;
10 }
11 void displayA()
12 {
13 std::cout<<a<<std::endl;
14 }
15 void display()
16 {
17 std::cout<<"base"<<std::endl;
18 }
19 };
20
21 class Derive : public Base
22 {
23
24 public:
25 int a;
26 int b;
27 Derive(int arg1, int arg2, int arg3):Base(arg1), a(arg2), b(arg3)
28 {}
29 void displayA()
30 {
31 std::cout<<a<<" "<<b<<std::endl;
32 }
33
34 void display()
35 {
36 std::cout<<"derive"<<std::endl;
37 }
38 };
39
40 int main()
41 {
42 // Base base(1);
43 // Derive derive(2, 3, 4);
44 // base.display();
45 // derive.display();
46 // derive.displayA();
47 Base base(1);
48 Derive derive(2, 3, 4);
49 base = derive;
50 base.display();
51 base.displayA();
52 return 0;
53 }
注意这里没有使用virtual ,类对象也不是指针或应用,运行结果为:
子类对象中只有与父类相对应的部分被赋给父类对象,且子类中同时保存了父类和子类的数据成员即使同名,所以base.display() base.displayA()输出的是父类中的部分
1 #include <iostream>
2 class Base
3 {
4
5 public:
6 int a ;
7 Base(int arg)
8 {
9 a = arg;
10 }
11 void displayA()
12 {
13 std::cout<<a<<std::endl;
14 }
15 virtual void display()
16 {
17 std::cout<<"base"<<std::endl;
18 }
19 };
20
21 class Derive : public Base
22 {
23
24 public:
25 int a;
26 int b;
27 Derive(int arg1, int arg2, int arg3):Base(arg1), a(arg2), b(arg3)
28 {}
29 void displayA()
30 {
31 std::cout<<a<<" "<<b<<std::endl;
32 }
33
34 virtual void display()
35 {
36 std::cout<<"derive"<<std::endl;
37 }
38 };
39
40 int main()
41 {
42 // Base base(1);
43 // Derive derive(2, 3, 4);
44 // base.display();
45 // derive.display();
46 // derive.displayA();
47 Base base(1);
48 Derive derive(2, 3, 4);
49 base = derive;
50 base.display();
51 base.displayA();
52
53 return 0;
54 }
结果同上!!
1 #include <iostream>
2 class Base
3 {
4
5 public:
6 int a ;
7 Base(int arg)
8 {
9 a = arg;
10 }
11 void displayA()
12 {
13 std::cout<<a<<std::endl;
14 }
15 virtual void display()
16 {
17 std::cout<<"base"<<std::endl;
18 }
19 };
20
21 class Derive : public Base
22 {
23
24 public:
25 int a;
26 int b;
27 Derive(int arg1, int arg2, int arg3):Base(arg1), a(arg2), b(arg3)
28 {}
29 void displayA()
30 {
31 std::cout<<a<<" "<<b<<std::endl;
32 }
33
34 virtual void display()
35 {
36 std::cout<<"derive"<<std::endl;
37 }
38 };
39
40 int main()
41 {
42 // Base base(1);
43 // Derive derive(2, 3, 4);
44 // base.display();
45 // derive.display();
46 // derive.displayA();
47 // Base base(1);
48 // Derive derive(2, 3, 4);
49 // base = derive;
50 // base.display();
51 // base.displayA();
52 Base *base = new Base(1);
53 Derive *derive = new Derive(2, 3, 4);
54 base = derive;
55 base->display();
56 base->displayA();
57 return 0;
58 }
以上使用了指针,运行结果为:
注意调用的是子类的成员!
也就是说java可以使用普通类,不需要像c++那样使用指针或引用,以及在父类函数前加关键字virtual就可以访问子类成员函数