| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1624 人关注过本帖
标题:一个典型的继承的例子!
只看楼主 加入收藏
飙马
Rank: 5Rank: 5
来 自:马里亚纳
等 级:贵宾
威 望:15
帖 子:779
专家分:280
注 册:2007-3-28
收藏
 问题点数:0 回复次数:3 
一个典型的继承的例子!

虽然很多地方可以见到这个例子,但还是忍不住贴出来,供大家尤其是新学者参考一下!这个例子不止是继承方面的好例子,而且两个Set和Get方法的使用也值得我们借签!

namespace CAR
{
public class Car//class of car,has attribute of 'weight' and 'speed'
{
private int weight;
private int speed;
public Car(int Weight,int Speed)
{ //相当公共属性
weight=Weight;
speed=Speed;
}
public void setweight(int Weight)
{
weight=Weight;
}
public void setspeed(int Speed)
{
speed=Speed;
}
public int getspeed()
{
return speed;
}
public int getweight()
{
return weight;
}
};


public class Sportcar:Car //inherit class of Car,has attributes of 'weight','speed','color' 运动车继承了车的属性,包括重量、速度、颜色
{
private string color;
public Sportcar(int Weight,int Speed,string Color):base(Weight,Speed)
{
setweight(Weight);
setspeed(Speed);
color=Color;
}
public void setcolor(string Color)
{
color=Color;
}
public string getcolor()
{
return color;
}
public static void Main()
{
Car car=new Car(100,100);
Sportcar sportcar=new Sportcar(100,200,"blcak");
Console.WriteLine("car's weight is "+car.getweight());
Console.WriteLine("car's speed is "+car.getspeed());
Console.WriteLine("sportcar's weight is "+sportcar.getweight());
Console.WriteLine("sportcar's speed is "+sportcar.getspeed());
Console.WriteLine("sportcar's speed is "+sportcar.getcolor());
}
}
}


[此贴子已经被作者于2007-5-16 9:38:55编辑过]

搜索更多相关主题的帖子: 例子 典型 继承 
2007-05-16 09:37
honkin
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-3-22
收藏
得分:0 
回复:(飙马)一个典型的继承的例子!
感觉像C++风格

一切从菜鸟做起!
2007-05-16 09:49
飙马
Rank: 5Rank: 5
来 自:马里亚纳
等 级:贵宾
威 望:15
帖 子:779
专家分:280
注 册:2007-3-28
收藏
得分:0 
无人回!

IT精英如同彩票:平凡的人像5块也中不到一样普遍,努力一点你中了5元保了个本。奖金越高,机率也就越小,付出的也越多,盖茨如同500万一样稀有。虽然每天忙碌而平凡,但我努力成为精英,做梦中了500万。
2007-05-17 16:07
chenkb730
Rank: 2
等 级:论坛游民
帖 子:39
专家分:44
注 册:2010-3-22
收藏
得分:0 
学习学习
2010-06-12 09:09
快速回复:一个典型的继承的例子!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020501 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved