首先明确一下:java中方法和函数是一个概念。并且你上面的代码完全可以执行,下面试测试代码:
public class MethodTest {
public static void main(String[] args) {
Person p=new Person("zhangsan",25);
p.speak();
}
}
class Person{
int age;
String name;
Person(String name){
this.name=name;
}
Person(String name,int age){
this.name =name;
this.age=age;
show();
}
public void speak(){
System.out.println("name="+this.name+"....age="+age);
this.show();
}
public void show(){
System.out.println(this.name);
}
} |