类的组成:
成员变量
成员方法
构造方法
无参构造方法
有参构造方法
构造方法的类型:
根据返回值:
void类型
非void类型
形式参数:
空参方法
非空参方法
示例:
class Student {
public String getString() {
return "hello world!";
}
public void show() {
System.out.println("show");
}
public void method(String name) {
System.out.println(name);
}
public String function(String s1,String s2) {
return s1+s2;
}
}
测试:
class StudentDemo {
public static void main(String[] args) {
//创建对象
Student s = new Student();
//调用无参无返回值方法
s.show();
//调用无参有返回值方法
String result = s.getString();
System.out.println(result);
//调用带参无返回值的方法
s.method("孙悟空");
//调用有多个参数有返回值的方法
String result2 = s.function("hello","world");
System.out.println(result2);
}
}