程序员第一次来到编程世界,敲下的问候语一般都是:
在这里插入代码片 public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); } }第一行:public class 表示公开类,Hello 是类名,注意区分大小写,{}中间是类的定义。
第二行:这里定义了一个main方法,string 是参数类型(字符串)args 是参数名,public static 修饰方法,表示公开的静态的,void 表示返回类型。
第三行:最后打印字符串到屏幕上。
public class People { // private // 属性 private String name; private int age; public People(String name, int age){ // 含参构造器 this.name = name; this.age = age; } public People(){ // 无参构造器 } public void setName(String newName){ this.name = newName; } public void setAge(int newAge){ this.age = newAge; } public String getName(){ return this.name; } public int getAge(){ return this.age; } }