保持快乐,善于表达,敢于创新

C#中类的继承和扩展

上一篇 / 下一篇  2008-03-03 10:52:14 / 个人分类:c#

using System;
class Vehicle   //基类
{
 public int wheels;
 protected float weight;
 public  Vehicle(){;}
 public Vehicle(int w, float g){
  wheels=w;
  
  weight=g;
  
 }
 public void show(){
  Console.WriteLine("the wheels of the Vehicle are {0}",wheels);
  Console.WriteLine("the weight of the Vehicle is {0}",weight);
  
  
  }
  
 }
class Train
{
 public int num;
 private int passenger;
 private float weight;
 public Train(){
  
  ;
  
  }
 public Train(int n, int p, float w){
  num=n;
  passenger=p;
  weight=w;
  
  
  }
 public void show(){
  Console.WriteLine("the num of the train {0}",num);
  Console.WriteLine("the passenger of the train {0}",passenger);
  Console.WriteLine("the weight of the train {0}",weight);
  
  
  
  }
 
 }
 
class Car:Vehicle{   //car 扩展于基类
    int passenger;
    public Car(int w, float g, int p):base(w,g) {   //注意这个语法
     wheels=w;
     weight=g;
     passenger=p;
     
     
     
     }
     new public void show(){
      Console.WriteLine("the wheels of the car is {0}",wheels);
      Console.WriteLine("the weight of the car is {0}",weight);
      Console.WriteLine("the passenger of the car is {0}",passenger);
      
      
      }

 

}

class Tes
{
 public static void Main(){
  Vehicle v1=new Vehicle(4,5);
  Train T1=new Train();
  Train T2=new Train(10,100,100);
  Car  C1=new Car(4,2,4);
  v1.show();
  T1.show();
  T2.show();
  C1.show();
  
  
  
  
  }
 
 
 
 
 }


TAG:

 

评分:0

我来说两句

Open Toolbar