zoukankan      html  css  js  c++  java
  • 封装装箱代码块

    一、快捷键:生成代码alt + shift + s

    二、复习:

    1、 static

    a. 可以修饰属性的方法:类属性/类方法,静态属性/方法

    b. 都属于类

    c. 被类的所有对象共享

     

    同种类型之间可以相互调用:

    a. 静态的可以直接调用静态的

    b. 非静态的可以直接调用非静态的

    c. 非静态的可以直接调用静态的

    d. 静态的可以通过对象调用非静态的

    示例:

     1 package 第五天;
     2 
     3 public class Demo {
     4     private static int s;
     5     private int i;
     6     
     7     public void fun() {
     8         System.out.println(i);
     9         System.out.println(s);
    10     }
    11     
    12     public static void main(String[] args) {
    13         System.out.println(s);
    14         Demo demo = new Demo();
    15         System.out.println(demo.i);
    16     }
    17 }

    2、封装类

    boolean Boolean
    byte Byte
    char Character
    short Short
    int Integer
    long Long
    float Float
    double Double

     

     

     

     

     

    原则:

      • 类不要放在默认包中
      • 优先使用封装类型

    示例:(定义Bean)

    JavsaBean:

    • 属性私有化并提供相应的getter/setter方法
    • 保证要有默认的构造方法
     1 package 第五天;
     2 
     3 public class stu {
     4     private Integer id;
     5     private String name;
     6     private boolean gender;
     7     private Float score;
     8     
     9     public stu() {
    10         
    11     }
    12     
    13     public stu(Integer id, String name, Boolean gender, Float score) {
    14         super();
    15         this.id = id;
    16         this.name = name;
    17         this.gender = gender;
    18         this.score = score;
    19     }
    20 
    21     /**
    22      * @return id
    23      */
    24     public Integer getId() {
    25         return id;
    26     }
    27 
    28     /**
    29      * @param id 要设置的 id
    30      */
    31     public void setId(Integer id) {
    32         this.id = id;
    33     }
    34 
    35     /**
    36      * @return name
    37      */
    38     public String getName() {
    39         return name;
    40     }
    41 
    42     /**
    43      * @param name 要设置的 name
    44      */
    45     public void setName(String name) {
    46         this.name = name;
    47     }
    48 
    49     /**
    50      * @return gender
    51      */
    52     public boolean isGender() {
    53         return gender;
    54     }
    55 
    56     /**
    57      * @param gender 要设置的 gender
    58      */
    59     public void setGender(boolean gender) {
    60         this.gender = gender;
    61     }
    62 
    63     /**
    64      * @return score
    65      */
    66     public Float getScore() {
    67         return score;
    68     }
    69 
    70     /**
    71      * @param score 要设置的 score
    72      */
    73     public void setScore(Float score) {
    74         this.score = score;
    75     }
    76 
    77     /* (非 Javadoc)
    78      * @see java.lang.Object#toString()
    79      */
    80     @Override
    81     public String toString() {
    82         return "stu [id=" + id + ", name=" + name + ", gender=" + gender + ", score=" + score + "]";
    83     }
    84 }

    测试代码:

     1 package 第五天;
     2 
     3 public class DemoTest {
     4     public static void main(String[] args) {
     5         stu stu1 = new stu();
     6         
     7         stu1.setId(111);
     8         stu1.setName("tom");
     9         stu1.setGender(true);
    10         stu1.setScore(99.9f);
    11         
    12         System.out.println(stu1);
    13         
    14         stu stu2 = new stu(222,"list", false, 77f);
    15         System.out.println(stu2);
    16     }
    17 }

     三、 正课

    1、 封装深入

     1 package 第七天;
     2 
     3 public class 封装深入 {
     4     public static void main(String[] args) {
     5         int a = 99;
     6         System.out.println(a + 1);
     7         System.out.println(a - 1);
     8         System.out.println(a * 1);
     9         System.out.println(a / 1);
    10         System.out.println(a % 1);
    11         System.out.println(a++);
    12         System.out.println(--a);
    13         System.out.println(a >> 1);
    14 
    15         Integer b = 4;
    16         System.out.println(b.MAX_VALUE);
    17         System.out.println(Integer.MIN_VALUE);
    18         
    19         int max = Integer.min(23, -89);
    20         System.out.println(max);
    21         String binaryString = Integer.toBinaryString(24);
    22         System.out.println(binaryString);
    23         System.out.println(Integer.toBinaryString(12345));
    24         
    25         char aa = Character.toUpperCase('a');  //变大写
    26         System.out.println(aa);
    27     }
    28 }

    2、 装箱折箱

    • 装箱:将基本数据类型的数据赋值给对应的引用数据类型;
    • 折箱:将引用数据类型的数据赋值给对应的基本数据类型;

    示例:

    package 盛开着永不凋零;
    
    public class 装箱折箱 {
        public static void main(String[] args) {
            int a = 34;
            Integer b = a;  //装箱
            int c = b;    //折箱
    
        }
        
        //在JDK7之前不能自动装箱折箱
    }

    3、 代码块:

    所有的java语句都必须写到代码块内。声明语句除外

    • 静态代码块:
    1 static {
    2         
    3     }
    • 构造代码快
    1 {
    2 
    3 }
    • 结论
      • 只要程序启动,就会运行静态代码模块中的代码
      • 静态代码块只运行一次
      • 当定义类的对象时,系统会依次调用:静态代码块---------构造代码快---------构造方法(其中,静态代码块只调用一次,构造方法块和构造方法是每
        new一次就调用一次
    • 示例:
     1 package 盛开着永不凋零;
     2 
     3 public class 装箱折箱示例 {
     4 public 装箱折箱示例() {
     5     System.out.println("gou zao daima");
     6 }
     7 {
     8     System.out.println("gou zao daima kuai");
     9 }
    10 static {
    11     System.out.println("jing tai daima kuai");
    12 }
    13 public static void main(String[] args) {
    14     new 装箱折箱示例();
    15     new 装箱折箱示例();
    16     new 装箱折箱示例();
    17 }
    18
  • 相关阅读:
    2020软件工程作业04
    2020软件工程作业03
    2020软件工程作业02
    2020软件工程作业01
    2020软件工程个人作业06——软件工程实践总结作业
    2020软件工程作业05
    2020软件工程作业02
    软件工程作业01
    2020软件工程个人作业06——软件工程实践总结作业
    2020软件工程作业05
  • 原文地址:https://www.cnblogs.com/yuandongshisan/p/11300257.html
Copyright © 2011-2022 走看看