Day11

static

代码一:
package oop.Demo10;

/**
* @author senko
* @date 2022/9/28 10:14
*/
//static
public class Student {
   private static int age;//静态的变量
   private double score;//非静态变量
   public void run(){
        go();//非静态方法可以直接调用静态方法的
}
   public static void go(){

}
   public static void main(String[] args) {
       Student s1 = new Student();
       System.out.println(Student.age);
       //System.out.println(Student.score);//错误,非静态变量不能通过类名调用
       System.out.println(s1.age);
       System.out.println(s1.score);

       new Student().run();
       Student.go();
  }
}
代码二:
package oop.Demo10;

/**
* @author senko
* @date 2022/9/28 10:27
*/
public class Person {
 //2:赋初始值
  {
       System.out.println("密名代码块");
  }
  //1:只执行一次
   static {
       System.out.println("静态代码块");
  }
  //3
   public Person() {
       System.out.println("构造方法");
  }

   public static void main(String[] args) {
       Person person=new Person();
       System.out.println("========================================");
       Person p = new Person();

  }
}
代码三:
package oop.Demo10;
import static java.lang.Math.random;  //静态导入包
import static java.lang.Math.PI;
/**
* @author senko
* @date 2022/9/28 10:35
*/
public class Test {
   public static void main(String[] args) {
       System.out.println(random());//使用静态导入包掉用的时候就不用写Math.random()
       System.out.println(PI);//使用静态导入包掉用的时候就不用写Math.PI
  }
}
 
posted @   宙斯xcl  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结
点击右上角即可分享
微信分享提示