1. 程式人生 > 其它 >100天程式碼提升計劃-第6天

100天程式碼提升計劃-第6天

前言

今天是星期六,學習與不學習也一直在我的腦海裡做鬥爭,學習與不學習都有各自的理由,最終依舊選擇學習,因為害怕一旦自己停下來就變得懶惰,變得懈怠,無論自己學一節課還是半節課也要堅持學下去。加油!!!

正文

靜態static關鍵字描述

一旦使用static關鍵字那麼內容不屬於自己屬於類

package Demo02;

public class Demo01Static {
    public static void main(String[] args) {
        Student one =new Student("張三",23);
        one.room ="101教室";
        System.out.println(
"姓名:"+one.getName()+",年齡:" +one.getAge()+",教室:"+one.room+",學號:"+one.getId()); Student two =new Student("李四",34); System.out.println("姓名:"+two.getName()+",年齡:" +two.getAge()+",教室:"+two.room+",學號:"+two.getId()); } }
package Demo02;

public class Student {
    
private String name;//姓名 private int age;//年齡 static String room;//所在教室 private int id; private static int jsq=100; public Student(){ this.id=++jsq; } //有參構造 public Student(String name, int age) { this.name = name; this.age = age; this.id=++jsq; }
//全參構造 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }