我是菜鸟 刚开始学程序 下面这段代码怎么编译都不成功,可是又看不出来错误,求大家帮我挑挑错,谢谢各位了。下面是我的代码:using System;
class Person{
private string name;
private string location;
Person(string name){
this.name = name;
location = "Beijing";
}
Person(string name,string location){
this.name = name;
this.location = location;
}
public string info(){
return "name is :" + name + " location is :" + location;
}
}class Student:Person{
private string school;
Student(string name,string school){
this(name,shcool,"shanghai"); //调用本类的另外的构造方法
}
Student(string name,string school,string location){
super(name,location);
this.school = school;
}
public string info(){
return super.info() + "school is :" + school;
}
}public class Test{
public static void Main(){
Person p1 = new Person("A");
Person p2 = new Person("B","湖南");
Student s1 = new Student("C","S1");
Student s2 = new Student("C","shanghai","S2");
Console.WriteLine(p1.info());
Console.WriteLine(p2.info());
Console.WriteLine(s1.info());
Console.WriteLine(s2.info());
}
}
下面是编译时的错误提示:ll.cs(31,16): warning CS0108:
“Student.info()”隐藏了继承的成员“Person.info()”。如果是有意隐藏,请
使用关键字 new。
ll.cs(16,16): (与前一个警告相关的符号位置)
ll.cs(23,2): error CS0122:
“Person.Person(string)”不可访问,因为它受保护级别限制
ll.cs(6,2): (与前一个错误相关的符号位置)
ll.cs(24,3): error CS0149: 应输入方法名称
ll.cs(24,13): error CS0103: 当前上下文中不存在名称“shcool”
ll.cs(26,2): error CS0122:
“Person.Person(string)”不可访问,因为它受保护级别限制
ll.cs(6,2): (与前一个错误相关的符号位置)
ll.cs(27,3): error CS0103: 当前上下文中不存在名称“super”
ll.cs(32,10): error CS0103: 当前上下文中不存在名称“super”
ll.cs(38,15): error CS1729: “Person”不包含采用“1”参数的构造函数
ll.cs(39,15): error CS1729: “Person”不包含采用“2”参数的构造函数
ll.cs(40,16): error CS1729: “Student”不包含采用“2”参数的构造函数
ll.cs(41,16): error CS1729: “Student”不包含采用“3”参数的构造函数
class Person{
private string name;
private string location;
Person(string name){
this.name = name;
location = "Beijing";
}
Person(string name,string location){
this.name = name;
this.location = location;
}
public string info(){
return "name is :" + name + " location is :" + location;
}
}class Student:Person{
private string school;
Student(string name,string school){
this(name,shcool,"shanghai"); //调用本类的另外的构造方法
}
Student(string name,string school,string location){
super(name,location);
this.school = school;
}
public string info(){
return super.info() + "school is :" + school;
}
}public class Test{
public static void Main(){
Person p1 = new Person("A");
Person p2 = new Person("B","湖南");
Student s1 = new Student("C","S1");
Student s2 = new Student("C","shanghai","S2");
Console.WriteLine(p1.info());
Console.WriteLine(p2.info());
Console.WriteLine(s1.info());
Console.WriteLine(s2.info());
}
}
下面是编译时的错误提示:ll.cs(31,16): warning CS0108:
“Student.info()”隐藏了继承的成员“Person.info()”。如果是有意隐藏,请
使用关键字 new。
ll.cs(16,16): (与前一个警告相关的符号位置)
ll.cs(23,2): error CS0122:
“Person.Person(string)”不可访问,因为它受保护级别限制
ll.cs(6,2): (与前一个错误相关的符号位置)
ll.cs(24,3): error CS0149: 应输入方法名称
ll.cs(24,13): error CS0103: 当前上下文中不存在名称“shcool”
ll.cs(26,2): error CS0122:
“Person.Person(string)”不可访问,因为它受保护级别限制
ll.cs(6,2): (与前一个错误相关的符号位置)
ll.cs(27,3): error CS0103: 当前上下文中不存在名称“super”
ll.cs(32,10): error CS0103: 当前上下文中不存在名称“super”
ll.cs(38,15): error CS1729: “Person”不包含采用“1”参数的构造函数
ll.cs(39,15): error CS1729: “Person”不包含采用“2”参数的构造函数
ll.cs(40,16): error CS1729: “Student”不包含采用“2”参数的构造函数
ll.cs(41,16): error CS1729: “Student”不包含采用“3”参数的构造函数
还有,C#里调用父类的方法和java不一样,不用Super,用base
构造函数前面加个public。
自类的info()加new;
调用自己的构造函数是在构造函数后面加:this(),调用基类的是:base();
调用基类的方法是base.方法名();
似乎就发现这么多,改改再试试~~