【发布时间】:2015-04-30 01:26:47
【问题描述】:
大家好,我需要一些帮助。我完全不知道如何使用继承来做到这一点。
目的是更改衬衫对象的父类(服装)继承的“名称”属性的值。
当通过调用 Shirt 的“ReNameShirt()”方法来更改名称并显示新的“名称”时,在我的 C# Web 窗体中单击按钮时。
我的老师暗示我说要使用子程序。还是迷路了。
你能帮帮我吗?非常感激。
服装类
using System;
//THE PARENT CLASS 'Clothing'
public class Clothing
{
public string _name;
public string _size;
public string name {get; set;}
public string size {get; set;}
}
//SUBCLASS OF 'Trousers'
public class Trousers : Clothing
{
public string LegLength { get; set; }
public Trousers()
{
LegLength = "91";
}
}
public class Shirt : Clothing
{
public string ReNameShirt()
{
Shirt Po = new Shirt();
Po.name = "blue shirt";
return ReNameShirt();
}
在 Inheritance.aspx.cs 文件中:
protected void Button2_Click(object sender, System.EventArgs e)
{
Shirt myShirt = new Shirt();
myShirt.name = "red polo";
myShirt.size = "85";
Label2.Text = "<b>Name:</b> " + myShirt.name + " <b>Size</b> " + myShirt.size;
myShirt.ReNameShirt();
Label3.Text = myShirt.size;
}
}
【问题讨论】:
-
你试过编译这段代码吗?您观察到了哪些错误,它们告诉了您什么?
标签: c# inheritance subroutine