在C#中,下列结构或者类定义正确的是(选一项)
A: public struct Person{ string name; int age; public void ShowName(){ Console.WriteLine(name); }}
B: public struct Person{ string name; int age; public Person(){ Conssole.WriteLine(name); }}
C: public class Person{ string name; int age; public Person(){ Conssole.WriteLine(name); }}
D: public class Person{ string name; int age; public Person(string name){ Conssole.WriteLine(name); }}
A: public struct Person{ string name; int age; public void ShowName(){ Console.WriteLine(name); }}
B: public struct Person{ string name; int age; public Person(){ Conssole.WriteLine(name); }}
C: public class Person{ string name; int age; public Person(){ Conssole.WriteLine(name); }}
D: public class Person{ string name; int age; public Person(string name){ Conssole.WriteLine(name); }}
举一反三
- (6-2)定义如下Person类,(1)处合理的代码是。 class Person { String name, department; int age; public Person(String n) { name = n; } public Person(String n, int a) { name = n; age = a; } public Person(String n, int a, String d) { ① department = d; } }
- 中国大学MOOC: public class Person { String name,department; int age; public Person(String n){name=n;} public Person(String n,int a){name=n; age=a;} public Person(String n, String d, int a ){ //调用2个参数的构造函数 department=d; }}
- 分析下面的程序,输出的结果是? public class Test { public static void main(String[] args) { final Person p = new Person("张三", 23); p.setName("李四"); p.setAge(24); System.out.println(p); } } class Person { private String name; private int age; public Person() { super(); } public Person(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "我的姓名是:" + name + ",我的年龄是:" + age ; } }
- 4.6 若有以下程序代码: class Person{ String name,department; int age; public Person(String n){name=n;} public Person(String n, int a){name=n; age=a;} public Person(String n, String d, int a ){ //执行此代码 } 下面那个选项可以添加到“//执行此代码”前面( )
- class Person{} class Student extends Person{ public int id; public int score; public String name; public int getScore() { return score; }