上面有几个小错误,改了大家再看看using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace ConsoleApplication2 { class Person { protected int id; protected string name; public Person(int pid, string pname) { id = pid; name = pname; } public virtual void infoPrint() { Console.WriteLine("Person's Id: {0}", id); Console.WriteLine("Person's Name: {0}", name); } } public class Student : Person { protected int class_id; protected int score; public Student(int pid, string pname,int sclass_id,int sscore) { id = pid; name = pname; class_id = sclass_id ; score = sscore; } public new void infoPrint() { Console.WriteLine("Person's Id: {0}", id); Console.WriteLine("Person's Name: {0}", name); Console.WriteLine("Person's Class_id: {0}", class_id); Console.WriteLine("Person's Score: {0}", score); } public class Teacher : Person { protected string position; protected string department; public Teacher(int pid, string pname, string tposition,string tdepartment) { id = pid; name = pname; position = tposition; department = tdepartment; } public override void infoPrint() { Console.WriteLine("Person's Id: {0}", id); Console.WriteLine("Person's Name: {0}", name); Console.WriteLine("Person's Position: {0}", position); Console.WriteLine("Person's Department: {0}", department); } } }static void Main(string[] args) { Person person=new Person(); Teacher teacher=new Teacher(86,"王宁","jiaoshou","jisuanji"); teacher.infoPrint(); Student student=new Student(101,"李刚",2,90); student.infoPrint(); }
}
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication2 { public class Person { protected int id; protected string name; public Person(int pid, string pname) { id = pid; name = pname; } public virtual void infoPrint() { Console.WriteLine("Person's Id: {0}", id); Console.WriteLine("Person's Name: {0}", name); } } public class Student : Person { protected int class_id; protected int score; public Student(int pid, string pname, int sclass_id, int sscore) : base(pid, pname) { class_id = sclass_id; score = sscore; } public new void infoPrint() { Console.WriteLine("Person's Id: {0}", id); Console.WriteLine("Person's Name: {0}", name); Console.WriteLine("Person's Class_id: {0}", class_id); Console.WriteLine("Person's Score: {0}", score); } } public class Teacher : Person { protected String position; protected string department; public Teacher(int pid, string pname, String tposition,String tdepartment) : base(pid, pname) { position = tposition; department = tdepartment; } public override void infoPrint() { Console.WriteLine("Person's Id: {0}", id); Console.WriteLine("Person's Name: {0}", name); Console.WriteLine("Person's Position: {0}", position); Console.WriteLine("Person's Department: {0}", department);
} }
public class Program { static void Main(string[] args) { Person person = new Person(1, "a"); Teacher teacher = new Teacher(86, "王宁", "jiaoshou", "jisuanji"); Student student = new Student(101, "李刚", 2, 90); } }
}
1、构造函数不继承,派生类会自动调用基类构造函数。 2、若类内没有定义构造函数,系统会自动隐式生成一个不带参数的构造函数,比如定义一个类: public class A { } 可以理解为它已经存在一个如下的构造函数public class A { public A() {
} } 3、派生类构造函数自动调用基类的不带参数的构造函数,注意下面的格式 public class B:A { public B() {
} } 相当于 public class B:A { public B():base() {
} }3、基类中带参数的构造函数必须显式调用,比如: public class A { public A() {
} public A(string str) {
}} public class B:A { public B():base("aaa") {
} }
为什么还显示错误 1 应输入 class、delegate、enum、interface 或 struct C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\projects\ConsoleApplication2\ConsoleApplication2\Program.cs 35 17 ConsoleApplication2 这个错误呢
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication2
{
class Person
{
protected int id;
protected string name;
public Person(int pid, string pname)
{
id = pid;
name = pname;
}
public virtual void infoPrint()
{
Console.WriteLine("Person's Id: {0}", id);
Console.WriteLine("Person's Name: {0}", name);
}
}
public class Student : Person
{
protected int class_id;
protected int score;
public Student(int pid, string pname,int sclass_id,int sscore)
{
id = pid;
name = pname;
class_id = sclass_id ;
score = sscore;
}
public new void infoPrint()
{
Console.WriteLine("Person's Id: {0}", id);
Console.WriteLine("Person's Name: {0}", name);
Console.WriteLine("Person's Class_id: {0}", class_id);
Console.WriteLine("Person's Score: {0}", score); }
public class Teacher : Person
{
protected string position;
protected string department;
public Teacher(int pid, string pname, string tposition,string tdepartment)
{
id = pid;
name = pname;
position = tposition;
department = tdepartment;
}
public override void infoPrint()
{
Console.WriteLine("Person's Id: {0}", id);
Console.WriteLine("Person's Name: {0}", name);
Console.WriteLine("Person's Position: {0}", position);
Console.WriteLine("Person's Department: {0}", department); }
}
}static void Main(string[] args)
{
Person person=new Person();
Teacher teacher=new Teacher(86,"王宁","jiaoshou","jisuanji");
teacher.infoPrint();
Student student=new Student(101,"李刚",2,90);
student.infoPrint();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
public class Person
{
protected int id;
protected string name;
public Person(int pid, string pname)
{
id = pid;
name = pname;
}
public virtual void infoPrint()
{
Console.WriteLine("Person's Id: {0}", id);
Console.WriteLine("Person's Name: {0}", name);
}
}
public class Student : Person
{
protected int class_id;
protected int score;
public Student(int pid, string pname, int sclass_id, int sscore)
: base(pid, pname)
{
class_id = sclass_id;
score = sscore;
}
public new void infoPrint()
{
Console.WriteLine("Person's Id: {0}", id);
Console.WriteLine("Person's Name: {0}", name);
Console.WriteLine("Person's Class_id: {0}", class_id);
Console.WriteLine("Person's Score: {0}", score); }
}
public class Teacher : Person
{
protected String position;
protected string department;
public Teacher(int pid, string pname, String tposition,String tdepartment) : base(pid, pname)
{
position = tposition;
department = tdepartment;
}
public override void infoPrint()
{
Console.WriteLine("Person's Id: {0}", id);
Console.WriteLine("Person's Name: {0}", name);
Console.WriteLine("Person's Position: {0}", position);
Console.WriteLine("Person's Department: {0}", department);
}
}
public class Program
{
static void Main(string[] args)
{
Person person = new Person(1, "a");
Teacher teacher = new Teacher(86, "王宁", "jiaoshou", "jisuanji");
Student student = new Student(101, "李刚", 2, 90); }
}
}
2、若类内没有定义构造函数,系统会自动隐式生成一个不带参数的构造函数,比如定义一个类:
public class A
{
}
可以理解为它已经存在一个如下的构造函数public class A
{
public A()
{
}
}
3、派生类构造函数自动调用基类的不带参数的构造函数,注意下面的格式
public class B:A
{
public B()
{
}
}
相当于
public class B:A
{
public B():base()
{
}
}3、基类中带参数的构造函数必须显式调用,比如:
public class A
{
public A()
{
}
public A(string str)
{
}}
public class B:A
{
public B():base("aaa")
{
}
}
这个错误呢
你可以把Main写在任意类中,但是你觉得写在Teacher、Person、Student中合适么?是不是要再写一个类?那么这个类叫什么好呢?不叫Program你打算叫什么?