参考代码:文章来源地址https://www.toymoban.com/news/detail-734961.html
using System;
namespace ClassInheritDemo
{
public class Parent
{
private int _assets;
private string _name;
private string _address;
protected int _faceScore;
protected int _iq;
public Parent(string name="",string address="",int assets=0)
{
_name = name;
_address = address;
_assets = assets;
_faceScore = 59;
_iq = 100;
}
public void HowManyIqAndFaceScore()
{
Console.WriteLine("{0} 智商:{1},颜值:{2} ", _name,_iq,_faceScore);
}
public int SubSomeAssets(int assets)
{
_assets -= assets;
return assets;
}
public void HowManyAssets()
{
Console.WriteLine("地址:{0} 名称: {1} 资产:{2} ",_address, _name,_assets);
}
}
public class Son:Parent
{
private int _assets;
private string _address;
public Son(string name, string address, int assets):base(name)
{
_address = address;
_assets = assets;
_faceScore = _faceScore+40;
_iq = _iq-20;
}
public void HowManyAssets()
{
Console.WriteLine("地址:{0} 资产:{1} ", _address, _assets);
}
public void AddSomeAssets(int assets)
{
_assets+= assets;
}
}
class Program
{
static void Main(string[] args)
{
Parent oldyang = new Parent("老杨", "北京", 5000);
Son youngyang = new Son("小杨", "西安", 0);
oldyang.HowManyIqAndFaceScore();
oldyang.HowManyAssets();
int assets = oldyang.SubSomeAssets(500);
Console.WriteLine("------------------");
youngyang.HowManyIqAndFaceScore();
youngyang.AddSomeAssets(assets);
youngyang.HowManyAssets();
Console.WriteLine("------------------");
oldyang.HowManyAssets();
}
}
}
文章来源:https://www.toymoban.com/news/detail-734961.html
到了这里,关于[C#基础训练]类的继承的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!