
求两道C#课后习题答案(接口类型题)
1.已知下面接口,试编写一个实现IIn接口的Square(正方形)类和一个实现IOut接口的Circle(圆)类;初始化圆类对象,通过接口将圆类对象的半径输出,作为正方形...
1.已知下面接口,试编写一个实现IIn接口的Square(正方形)类和一个实现IOut接口的Circle(圆)类;初始化圆类对象,通过接口将圆类对象的半径输出,作为正方形的边长,并通过接口传给正方形对象。
interface IIn
{
void In(int size);
void Show();
}
interface IOut
{
int Out();
void Show;
}
2.已知下面接口,试编写一个实现了该接口的Student类,使该类只能通过接口编程。
interface IStudent
{
string Name
{
get;
set;
}
string age
{
get;
set;
}
void learn();
void Show();
} 展开
interface IIn
{
void In(int size);
void Show();
}
interface IOut
{
int Out();
void Show;
}
2.已知下面接口,试编写一个实现了该接口的Student类,使该类只能通过接口编程。
interface IStudent
{
string Name
{
get;
set;
}
string age
{
get;
set;
}
void learn();
void Show();
} 展开
展开全部
class Square : IIn{
int Size;
#region IIn Members
public void In(int size) {
this.Size = size;
}
public void Show() {
Console.WriteLine("正方形面积为:" + Size*Size);
}
#endregion
}
class Circle : IOut {
int R;
public Circle(int r) {
this.R = r;
}
#region IOut Members
public int Out() {
return this.R;
}
public void Show() {
Console.WriteLine("园面积为:" + 3.14 * R * R);
}
#endregion
}
class Student : IStudent {
string age;
string name;
#region IStudent Members
string IStudent.Name {
get {
return name;
}
set {
name = value;
}
}
string IStudent.Age {
get {
return age;
}
set {
age = value;
}
}
void IStudent.learn() {
Console.WriteLine("learning...");
}
void IStudent.Show() {
Console.WriteLine(name + " " + age);
}
#endregion
}
int Size;
#region IIn Members
public void In(int size) {
this.Size = size;
}
public void Show() {
Console.WriteLine("正方形面积为:" + Size*Size);
}
#endregion
}
class Circle : IOut {
int R;
public Circle(int r) {
this.R = r;
}
#region IOut Members
public int Out() {
return this.R;
}
public void Show() {
Console.WriteLine("园面积为:" + 3.14 * R * R);
}
#endregion
}
class Student : IStudent {
string age;
string name;
#region IStudent Members
string IStudent.Name {
get {
return name;
}
set {
name = value;
}
}
string IStudent.Age {
get {
return age;
}
set {
age = value;
}
}
void IStudent.learn() {
Console.WriteLine("learning...");
}
void IStudent.Show() {
Console.WriteLine(name + " " + age);
}
#endregion
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询