public class Test1
{
public static void main(String[] args) {
b aa=new b();
}
}
class a
{
int num=0;
String name;
public a(){
System.out.println("大家好!我叫:");
}
}
class b extends a
{
String name;
public b(){
int shu=2;
this.name="张三"; //这是第2句
System.out.println("我叫"+name+"我今年"+shu+"岁。");
}
}
在Java书中说this在构造函数中只能出现在第一句,可是这里在第二句也可以正常运行啊,求大神讲解this
{
public static void main(String[] args) {
b aa=new b();
}
}
class a
{
int num=0;
String name;
public a(){
System.out.println("大家好!我叫:");
}
}
class b extends a
{
String name;
public b(){
int shu=2;
this.name="张三"; //这是第2句
System.out.println("我叫"+name+"我今年"+shu+"岁。");
}
}
在Java书中说this在构造函数中只能出现在第一句,可是这里在第二句也可以正常运行啊,求大神讲解this
函数,对象要区分
还有楼主你应该是看错了` 没有什么this只能出现在构造函数第一句这种东西`
class Person{
String name;
int age;
public Person(){
System.out.println("student");
}
public Person(String name,int age){
this();
this.name = name;
this.age = age;
System.out.println("worker");
}
}public class TestThis { public static void main(String[] args) {
new Person("zhangsan",25);
}}像这种调用构造函数的时候this()必须放在第一行,不然就会报错