C# 接口问题如下
3、设计一个控制台应用程序,定义一个接口。具体要求如下:(1)定义一个接口IShowInfo,接口包含1个voidShowInfo()成员方法,3个分别用于显示身份证号、...
3、设计一个控制台应用程序,定义一个接口。具体要求如下:
(1)定义一个接口IShowInfo,接口包含1个void ShowInfo( )成员方法,3个分别用于显示身份证号、姓名、性别的属性。
(2)定义一个Person类,继承接口IShowInfo,实现接口成员,并且设置ShowInfo方法为虚方法输出Person类对象详细信息,定义1个有3个参数的构造函数,用于初始化3个私有成员。
(3)定义一个Student子类继承Person父类,Student类中定义1个公有属性用来访问学号私有成员,重写ShowInfo方法输出Student类对象详细信息。定义1个有4个参数的构造函数,用于初始化4个成员。
(4)Main方法中调试并调用子类对象及方法。
-------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Student s = new Student();
s.Xuehao = 123;
}
}
public interface IShowInfo
{
void ShowInfo();
int ID { get; set; }
string name { get; set; }
string sex { get; set; }
}
class Person : IShowInfo
{
private int ID;
public int ID1
{
get { return ID; }
set { ID = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
public Person(int ID, string name, string sex)
{
this.ID1 = ID;
this.Name = name;
this.Sex = sex;
}
public void ShowInfo()
{
Console.WriteLine();
}
}
class Student : Person
{
public int ID;
string name;
string sex;
private int xuehao;
public int Xuehao
{
get { return xuehao; }
set { xuehao = value; }
}
public Student(int ID, string name, string sex,int Xuehao) :base()
{
this.xuehao = Xuehao;
}
new void ShowInfo()
{
Console.WriteLine("" + Xuehao + "" + name + "" + ID + " " + sex);
}
}
} 展开
(1)定义一个接口IShowInfo,接口包含1个void ShowInfo( )成员方法,3个分别用于显示身份证号、姓名、性别的属性。
(2)定义一个Person类,继承接口IShowInfo,实现接口成员,并且设置ShowInfo方法为虚方法输出Person类对象详细信息,定义1个有3个参数的构造函数,用于初始化3个私有成员。
(3)定义一个Student子类继承Person父类,Student类中定义1个公有属性用来访问学号私有成员,重写ShowInfo方法输出Student类对象详细信息。定义1个有4个参数的构造函数,用于初始化4个成员。
(4)Main方法中调试并调用子类对象及方法。
-------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Student s = new Student();
s.Xuehao = 123;
}
}
public interface IShowInfo
{
void ShowInfo();
int ID { get; set; }
string name { get; set; }
string sex { get; set; }
}
class Person : IShowInfo
{
private int ID;
public int ID1
{
get { return ID; }
set { ID = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
public Person(int ID, string name, string sex)
{
this.ID1 = ID;
this.Name = name;
this.Sex = sex;
}
public void ShowInfo()
{
Console.WriteLine();
}
}
class Student : Person
{
public int ID;
string name;
string sex;
private int xuehao;
public int Xuehao
{
get { return xuehao; }
set { xuehao = value; }
}
public Student(int ID, string name, string sex,int Xuehao) :base()
{
this.xuehao = Xuehao;
}
new void ShowInfo()
{
Console.WriteLine("" + Xuehao + "" + name + "" + ID + " " + sex);
}
}
} 展开
1个回答
展开全部
你的代码基本思路是对的,但有很多错误
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication2
{
//接口
public interface IShowInfo
{
void ShowInfo();
int ID { get; set; }
string Name { get; set; }
string Sex { get; set; }
}
//Person类
public class Person : IShowInfo
{
int id;
string name;
string sex;
public Person(int id, string name, string sex)
{
this.id =id;
this.name=name;
this.sex=sex;
}
public virtual void ShowInfo()
{
Console.WriteLine("姓名:{0}", this.Name);
Console.WriteLine("编号:{0}", this.ID);
Console.WriteLine("性别:{0}", this.Sex); ;
}
public int ID
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Sex
{
get { return sex; }
set { sex = value; }
}
}
// Student 类
public class Student : Person
{
private int studentID;
public Student(int id, string name, string sex, int studentID)
: base(id, name, sex)
{
this.studentID = studentID;
}
public override void ShowInfo()
{
base.ShowInfo();
Console.WriteLine("学号:{0}", this.StudentID);
}
public int StudentID
{
get { return studentID; }
set { studentID = value; }
}
}
class Program
{
static void Main(string[] args)
{
// 测试子类对象及方法
Person student = new Student(1, "张三", "男", 1201);
student.ShowInfo();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询