虚方法
using System;
namespace app{
class MyClass{
public static void Main(string[] args){
B b= new B();
A a= new A();
Console.WriteLine(a.a(13));
Console.WriteLine(b.a(13));
}
}
class A{
int b=1;
public int a(){
return b;
}
public virtual int a( int _b){ //虚方法
return b=_b;
}
}
class B:A{
public override int a (int _b){
return base.a(88);
}
}
}
25
25
1
using System;
2
namespace app{
3
class MyClass{
4
public static void Main(string[] args){
5
B b= new B();
6
A a= new A();
7
Console.WriteLine(a.a(13));
8
Console.WriteLine(b.a(13));
9
}
10
}
11
class A{
12
int b=1;
13
public int a(){
14
return b;
15
}
16
public virtual int a( int _b){ //虚方法
17
return b=_b;
18
}
19
}
20
class B:A{
21
public override int a (int _b){
22
return base.a(88);
23
}
24
}
25
}
using System;
namespace app{
class MyClass{
public static void Main(string[] args){
B b= new B();
A a= new A();
Console.WriteLine(a.a(13));
Console.WriteLine(b.a(13));
}
}
class A{
int b=1;
public int a(){
return b;
}
public int a( int _b){ //虚方法
return b=_b;
}
}
class B:A{
public new int a (int _b){ //
return base.a(88);
}
}
}
25
25
1
using System;
2
namespace app{
3
class MyClass{
4
public static void Main(string[] args){
5
B b= new B();
6
A a= new A();
7
Console.WriteLine(a.a(13));
8
Console.WriteLine(b.a(13));
9
}
10
}
11
class A{
12
int b=1;
13
public int a(){
14
return b;
15
}
16
public int a( int _b){ //虚方法
17
return b=_b;
18
}
19
}
20
class B:A{
21
public new int a (int _b){ //
22
return base.a(88);
23
}
24
}
25
}
using System;
namespace app{
class MyClass{
public static void Main(string[] args){
B b= new B();
A a= new A();
Console.WriteLine(a.a(13));
Console.WriteLine(b.a(13));
}
}
class A{
int b=1;
public int a(){
return b;
}
public virtual int a( int _b){ //虚方法
return b=_b;
}
}
class B:A{
public override sealed int a (int _b){
return base.a(88);
}
}
class C:B{
public override int a (int _b){ //报错
return base.a(88);
}
}
}
30
30
1
using System;
2
namespace app{
3
class MyClass{
4
public static void Main(string[] args){
5
B b= new B();
6
A a= new A();
7
Console.WriteLine(a.a(13));
8
Console.WriteLine(b.a(13));
9
}
10
}
11
class A{
12
int b=1;
13
public int a(){
14
return b;
15
}
16
public virtual int a( int _b){ //虚方法
17
return b=_b;
18
}
19
}
20
class B:A{
21
public override sealed int a (int _b){
22
return base.a(88);
23
}
24
}
25
class C:B{
26
public override int a (int _b){ //报错
27
return base.a(88);
28
}
29
}
30
}
using System;
namespace app {
class MyClass {
public static void Main (string[] args) {
B b = new B ();
A a = new A ();
a.a ();
b.a ();
a = b; //访问基类的方法
a.a ();
}
}
class A {
public void a () { //虚方法
Console.WriteLine ("虚方法!");
}
}
class B : A {
public new void a () {
Console.WriteLine ("新虚方法!");
}
}
}
23
1
using System;
2
namespace app {
3
class MyClass {
4
public static void Main (string[] args) {
5
B b = new B ();
6
A a = new A ();
7
a.a ();
8
b.a ();
9
a = b; //访问基类的方法
10
a.a ();
11
}
12
}
13
class A {
14
public void a () { //虚方法
15
Console.WriteLine ("虚方法!");
16
}
17
}
18
class B : A {
19
public new void a () {
20
Console.WriteLine ("新虚方法!");
21
}
22
}
23
}
using System;
class Dog{
public static void Main(string[] args){
B b = new B();
A a= new A();
b.a();
a.a();
a=b;
a.a();//B类的方法
}
}
class A{
public virtual void a(){
Console.WriteLine("A.a方法");
}
}
class B:A{
public override void a(){
Console.WriteLine("B.a方法");
}
}
23
1
using System;
2
class Dog{
3
public static void Main(string[] args){
4
B b = new B();
5
A a= new A();
6
b.a();
7
a.a();
8
a=b;
9
a.a();//B类的方法
10
}
11
}
12
class A{
13
public virtual void a(){
14
Console.WriteLine("A.a方法");
15
}
16
}
17
18
class B:A{
19
public override void a(){
20
Console.WriteLine("B.a方法");
21
}
22
}