Java 入门练习:类与对象

Java 入门练习:类与对象

HelloWorld

打印HelloWorld

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

计算1到100的奇数和


public class Zerotone {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 100; i +=2) {
            sum += i;
        }
        System.out.println("1到100的奇数和是"+sum);

    }
}

设置Student类

口令即Password

class Students{
    private String name;
    private String password;

    String getName() {
        return name;
    }
    void setName(String name) {
        this.name = name;
    }
    String getPassword() {
        return password;
    }
    void setPassword(String password) {
        this.password = password;
    }
    String updateName(String name){
        this.setName(name);
        return this.name;
    }
}


public class student {
    public static void main(String[] args)
    {
    Students s=new Students();
    s.setName("张三");
    s.setPassword("hangman");
    System.out.println("用户名:"+s.getName());
    System.out.println("口令:"+s.getPassword());
    System.out.println("修改后的用户名为:");
    s.updateName("王五");
    System.out.println(s.getName());
    }
}
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页