5.4

this关键字代表的含义是 ? 该如何使用
  this关键字: 当前对象的引用
  大多数情况用来解决传入的参数和成员变量重名的现象
  可以通过this关键字来调用当前类中的其他的构造方法   this();
static关键字代表的含义是 ? 该如何使用 ?
  static: 静态的, 可以修饰变量(成员变量, 方法, 类)
  修饰变量:
    static修饰的变量不存在于栈空间和堆空间, 存在于数据区
    不管实例化多少个对象, 所有的对象都共享一份
    static修饰的变量在访问的时候不需要实例化, 直接拿类名去点出来

  修饰方法:
    用途, 做一些工具类的使用
    因为不需要实例化, 使用起来比较方便
  注意:
    static修饰的方法不能访问非静态成员!!!

public class Student {
    private String name;
    public static String school;    //学校
    public Student() {
        // this("");
        System.out.println("空参的构造方法 !");
        
    }
    
    public static void showNameInfo(String name) {
        char c = school.charAt(0);
/*        char c = name.charAt(0);
        String n = name.substring(1);
        System.out.println("学生姓: " + c);
        System.out.println("学生名: " + n);*/
    }

    public void showInfo2() {
        
    }
    
    public Student(String name) {
        this();
        this.name = name;
    }
    
    public void setName(String name) {
        // this();
        this.name = name;
    }
    
    public String getName() {
        return this.name;
    }
}

打印

public class Test {

    public static void main(String[] args) {    
        
        // TODO Auto-generated method stub
/*        Student s1 = new Student("小红");
        Student s2 = new Student("小明");
        
        Student.school = "山东理工大";  //静态成员变量拿类名调用
        
        System.out.println(Student.school);
        
        Student.showNameInfo(s1.getName());
        // Math.
        
        m();*/
    }
    

11, 单例模式介绍
12, 文档注释的使用
练习: 能自己写出单例模式

猜你喜欢

转载自www.cnblogs.com/syx1997/p/8991000.html
5.4