inheritproperties.cs

来自「vc的原码例子12 vc的原码例子12」· CS 代码 · 共 85 行

CS
85
字号
using System;

class Package
{
	public Package(int W)
	{
		this.Weight = W;
	}

	private int weight;

	public int Weight
	{
		set 
		{	 
			if ((value >= 25) && (value <= 50)) 
				weight = value;
			else
				weight = -1;
		}

		get
		{
			return weight;
		}   
	}

	public void showPackage()
	{
		Console.WriteLine("Weight: {0}", Weight);
	}
}

class CardboardBox : Package
{
	public CardboardBox(int weight, int paperweight) : base(weight)
	{
		PaperWeight = paperweight;  // Strength of cardboard
	}

	public void ShowBox()
	{
		Console.WriteLine("Box supports {0} pounds", BoxWeight);
		base.showPackage();
	}

	private int BoxWeight;
	
	public int PaperWeight
	{
		set 
		{	 
			if ((value > 0) && (value <= 100)) 
				BoxWeight = value;
			else
				BoxWeight = -1;
		}

		get
		{
			return BoxWeight;
		}   
	}
}


class clsInheritProperties
{
	public static void Main ()
	{
		CardboardBox Box = new CardboardBox(50, 75);
		Box.ShowBox();

		Box.PaperWeight = 200;
		Box.ShowBox();

		Box.PaperWeight = 80;
		Box.Weight = 33;
		Box.ShowBox();

		Box.PaperWeight = 50;
		Box.Weight = 200;
		Box.ShowBox();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?