构造方法

特点

  • 没有返回类型,也不能定义void;
  • 名称同本类相同;
  • 主要作用:完成对象的初始化工作,能把对应对象的参数传给对象成员。
class B{
    public B(){   //无参构造方法
    }


    public B(int a,int b){   //有参构造方法
    }
}
public class Cellphone {
    
    /*public Cellphone() {
        System.out.println("智能手机的默认语言为英文");
    }
    
    public Cellphone(String defaultLanguage) {
        System.out.println("将智能手机的默认语言设置为" + defaultLanguage);
    }

    public static void main(String[] args) {
        Cellphone cellphone1 = new Cellphone();
        Cellphone cellphone2 = new Cellphone("中文");
    }*/
    public static void main(String[] args) {
        String lan = "中文";
        Cellphone cp = new Cellphone();
        Cellphone cp2 = new Cellphone(lan);

    }

    public Cellphone(String lan){
        System.out.println("智能手机的语言可以设置为:"+lan);
    }

    public Cellphone(){
        System.out.println("智能手机的默认语言为英文");
    }



}

 this关键字

this关键字被隐式地用于引用对象的成员变量和成员方法。

public class Teacher {
    static String tnm = "A";
    String nm= "AB";

    public static void main(String[] args) {
        System.out.println("在main中直接访问class变量,变量定义时需有static字段,其名称:" + tnm);

        Teacher tc = new Teacher();
        System.out.println("在main中,访问class成员变量,可通过对象.成员变量名称访问:"+tc.nm);

        String nm = "B";
        tc.setname(nm);
        tc.setname2(nm);
    }

    public void setname(String nm) {
        System.out.println("\nclass变量:"+this.nm);
        System.out.println("不更新class中变量的值,传值名称:" +nm);
        System.out.println("class变量未更新:" +this.nm);
    }

    public void setname2(String name) {
        System.out.println("\nclass变量:"+this.nm);
        this.nm = name;
        System.out.println("更新class中变量的值,传值名称:" +nm);
        System.out.println("class中变量已更新:" +this.nm);
    }
}


  /*
在main中直接访问class变量,变量定义时需有static字段,其名称:A
在main中,访问class成员变量,可通过对象.成员变量名称访问:AB

class变量:AB
不更新class中变量的值,传值名称:B
class变量未更新:AB

class变量:AB
更新class中变量的值,传值名称:B
class中变量已更新:B
*/

return this的目的、作用、使用场景

public class Teacher {
    String nm= "AB";
    int age = 30;

    public static void main(String[] args) {

        Teacher tc = new Teacher();

        String nm = "B";
        int age = 20;
       /* 因为每个方法均返回了一个该类对应的对象,所以方法执行可简化。
        tc.setname(nm).setname2(nm).setage(age); 同以下三行一个目的。

        tc.setname(nm);
        tc.setname2(nm);
        tc.setage(age);

        */
        tc.setname(nm).setname2(nm).setage(age);
    }

    public Teacher setname(String nm) {
//        return为本类,而非String、int等,所以 返回类型为本类名:Teacher
        System.out.println("\nclass变量:"+this.nm);
        System.out.println("不更新class中变量的值,传值名称:" +nm);
        System.out.println("class变量未更新:" +this.nm);
        /* this关键字引用的就是本类的一个对象,
        类是无法返回的,需返回累的一个对象,如 方法返回的为String,return nm。同理,该场景则return this。
       当返回的是一个对象时,该方法后可连着执行方法,从而简化代码。
         */
        return this; 
    }

    public Teacher setname2(String name) {
        System.out.println("\nclass变量:"+this.nm);
        this.nm = name;
        System.out.println("更新class中变量的值,传值名称:" +nm);
        System.out.println("class中变量已更新:" +this.nm);
        return this;
    }

    public Teacher setage(int age) {
        System.out.println("\nclass变量:"+this.age);
        this.age = age;
        System.out.println("更新class中变量的值,传值名称:" +age);
        System.out.println("class中变量已更新:" +this.age);
        return this;
    }

}


  /*
class变量:AB
不更新class中变量的值,传值名称:B
class变量未更新:AB

class变量:AB
更新class中变量的值,传值名称:B
class中变量已更新:B

class变量:30
更新class中变量的值,传值名称:20
class中变量已更新:20
*/

使用this在无参构造方法中调用有参构造方法

public class EggCake {
    int num=1;

    public static void main(String[] args) {
        EggCake ec = new EggCake();

        System.out.println("此为有参构造方法");
        EggCake ec2 = new EggCake(5);

    }

    public  EggCake(int num){
//        有参构造方法
        System.out.println("一共有:"+num+"个");
    }

    public  EggCake(){
//    无参构造方法,使用this调用有参构造方法,注:this前不应该有任何代码
        this(2);
        System.out.println("此为无参构造方法\n");
    }

}

/*
结果
        一共有:2个
        此为无参构造方法

        此为有参构造方法
        一共有:5个*/

static关键字

static可修饰变量、常量、方法,为静态变量、静态常量、静态方法。

静态变量

共享变量使用static修饰,则为静态变量,调用方法:类名.静态变量名称,不需创建对象即可访问。

作用范围:整个程序的生命周期,当程序结束后,静态变量销毁。(全局非静态变量作用是整个类,随着类的销毁而销毁)

 

public class Pool { // 创建水池类

/*int water = 0;  //非静态变量
    public static void main(String[] args) {
         Pool pool = new Pool();
        int addc = 2;
        int subc = 2;
        System.out.println("初始:"+pool.water); //访问时,对象.成员名称
        pool.add(addc);
        System.out.println("添加:"+addc+"次后:"+pool.water);
        pool.subtract(subc);
        System.out.println("减少:"+subc+"次后:"+pool.water);
    }*/

    static int water = 0; //静态变量
    public static void main(String[] args) {
        Pool pool = new Pool();
        int addc = 2;
        int subc = 2;
        System.out.println("初始:"+Pool.water); //访问时,类.静态变量名称   本类下直接访问静态变量名称可行。
        pool.add(addc);
        System.out.println("添加:"+addc+"次后:"+Pool.water);
        pool.subtract(subc);
        System.out.println("减少:"+subc+"次后:"+Pool.water);
    }


    public void add (int count){ //加水 count 次数 每次加3
        for (int i = 1; i <= count ; i++) {
            water+= 3;
        }
    }

    public void subtract (int count){ //加水 count 次数 每次减水2
        for (int i = 1; i <= count ; i++) {
            if (water >= 2) {
                water-= 2;
            }
        }
    }
}

静态方法

调用:类名.静态方法名称(); 不需创建对象,即可访问。

public class Pool { // 创建水池类

    static int water = 0; //静态变量
    public static void main(String[] args) {
        int addc = 2;
        int subc = 2;
        System.out.println("初始:"+Pool.water); //访问时,类.静态变量名称   本类下直接访问静态变量名称可行。
        Pool.add(addc);
        System.out.println("添加:"+addc+"次后:"+Pool.water);
        Pool.subtract(subc);//调用静态方法 类.静态方法
        System.out.println("减少:"+subc+"次后:"+Pool.water);
    }


    public static void add (int count){ //加水 count 次数 每次加3
        for (int i = 1; i <= count ; i++) {
            water+= 3;
        }
    }

    public static void subtract (int count){ //加水 count 次数 每次减水2
        for (int i = 1; i <= count ; i++) {
            if (water >= 2) {
                water-= 2;
            }
        }
    }
}

静态代码块

完成类的初始化操作,在声明类的时候即执行。

public class StaticTest{

    static { //静态代码块, 对象声明时执行
        System.out.println("静态代码块:对象声明时执行");
    }

    { //非静态代码块, 对象赋值时执行
    System.out.println("非静态代码块: 对象new时执行");
    }

    StaticTest(){ //无参构造方法
        System.out.println("无参构造方法");
    }

    StaticTest(int n){ //有参构造方法
        System.out.println("有参构造方法:"+n);
    }

    public static void main(String[] args) {
        System.out.println("对象声明啦");
        StaticTest s1;

        System.out.println("\n对象new啦:默认调用无参构造方法");
        StaticTest s2 = new StaticTest();

        System.out.println("\n对象new啦:加入参数则调用有参构造方法");
        StaticTest s3 = new StaticTest(2);
        
    }
}

  /*      静态代码块:对象声明时执行
        对象声明啦

        对象new啦:默认调用无参构造方法
        非静态代码块: 对象new时执行
        无参构造方法

        对象new啦:加入参数则调用有参构造方法
        非静态代码块: 对象new时执行
        有参构造方法:2*/

主方法 

public class TestMain {
    public static void main(String[] args) { // 定义主方法
        for (int i = 0; i < args.length; i++) { // 根据参数个数做循环操作
            System.out.println(args[i]); // 循环打印参数内容
        }
    }
}
/* 1 3 7 Process finished with exit code 0*/

 

posted on 2022-07-07 19:23  jxba  阅读(35)  评论(0编辑  收藏  举报