zoukankan      html  css  js  c++  java
  • 构造方法,this关键字,static关键字,封装,静态变量

    1.构造方法

    构造方法是一种特殊的方法,是专门用于创建/实例化对象的方法。

    构造方法根据是否有参数分为两类:1.无参构造方法  2.有参构造方法

    1.1无参构造方法

    无参构造方法就是构造方法中没有参数。构造方法在创建对象(new Dog())时使用.无参构造方法一般用于给属性赋默认值。如果开发中没有定义无参构造方法,jvm默认给类分配一个无参构造方法。格式如下

    [修饰符] 类名(){
    
    }

    1.2有参构造方法

    当构造/实例化一个对象的时候,可以向构造方法中传递参数,这样的构造方法叫做有参构造方法。有参构造方法格式如下

    [修饰符] 类名(type arg1,type arg2,...){
        //初始化代码
    }

    有参构造和无参构造是方法重载的关系

    2.1局部变量和成员变量的优先级

    在一个代码块(作用域)中,局部变量和成员变量同名,局部变量的优先级高于成员变量。

    2.2有参构造常见问题

    在一个类中,当定义了有参构造的时候,jvm不再默认分配无参构造。但是在一些情况下我们仍然需要用到无参构造方法,如反射,只能用无参构造,不能用有参构造。所以,一般情况下,我们在定义有参构造的时候,同时也会定义无参构造。

    3this关键字(上)

    this关键字一般指向对象本身。this关键字用于访问成员属性,可解决局部变量和成员变量同名时优先级的问题。如下

    1 public Dog2(String name,int health,int love,String strain){
    2 System.out.println("this:"+this);
    3     this.name = name;
    4     this.health = health;
    5     this.love = love;
    6     this.strain = strain;
    7 }
    1 public class Test04{
    2     public static void main(String[] args){
    3         
    4         Dog2 dog = new Dog2("二狗",100,0,"土狗");
    5         System.out.println("dog:"+dog);
    6         dog.showInfo();
    7     }
    8 }

    通过打印this的地址,和dog类的地址。可以发现他们都指向同一个地址。由此可知,this指向对象本身。

    4static关键字

    4.1静态变量

    static关键字,表示静态的,可以修饰变量,也可以修饰方法。

    static修饰的变量称为静态变量,形式如下:

    static type 变量名 [=初始值]

    静态变量,也叫类变量,归类所有。存放在方法区(共享区)中的静态区中,可以被类的对象共享访问。

    静态变量的访问方式:

    类名.静态变量(推荐)

    对象名.静态变量

    通过以下需求加深理解:

    需求:统计汽车工厂成产了多少台汽车?

     1 public class Car{
     2     String brand;
     3     String type;
     4     float price;
     5 
     6     static int count = 0;
     7     
     8     public Car(){
     9         Car.count++;
    10     }
    11     
    12     public Car(String brand,String type,float price){
    13         this.brand = brand;
    14         this.type = type;
    15         this.price = price;
    16         Car.count++;
    17     }
    18     
    19     public void showInfo(){
    20         System.out.println("车辆信息:");
    21         System.out.println("品牌:"+this.brand);
    22         System.out.println("型号:"+this.type);
    23         System.out.println("价格:"+this.price);
    24         System.out.println("我是第"+Car.count+"辆车");
    25     }
    26     
    27     
    28 }
     1 public class Test01{
     2     public static void main(String[] args){
     3         Car car1 = new Car("奔驰","漏油GL300",66);
     4         car1.showInfo();
     5         
     6         
     7         Car car2 = new Car("奔驰","漏油GL400",66);
     8         car2.showInfo();
     9         
    10         System.out.println(Car.count);
    11         System.out.println(car1.count);
    12         System.out.println(car2.count);
    13         
    14     }
    15 }

    类中包含静态成员(静态属性,静态方法)和实例成员(实例变量,实例方法)。

    4.2静态方法

    static修饰的方法称为静态方法,形式如下:

    [修饰符] static 返回值类型 方法名(arg..){
    
    }

    静态方法归类所有,调用方式:

    类名.方法名()(推荐)

    对象名.方法名()

    需注意的是:实例方法可以访问静态成员。

          静态方法不能访问非静态成员。

    5封装

    封装:将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问。

    封装的步骤:[1]属性私有化

          [2]提供公共的设置器和访问器

          [3]在设置器和访问器中添加业务校验逻辑

    例子如下:

     1 public class Dog{
     2     
     3     // 【1】private 私有的,对外不可见
     4     private String name;
     5     private int health;
     6     private int love;
     7     private String strain;
     8 
     9     // 【2】提供公共的设置器(setter)和访问器(getter)
    10     public void setName(String name){
    11         // 【3】逻辑校验
    12         if(name.equals("")){
    13             System.out.println("姓名不能为空.");
    14         }else{
    15             this.name = name;
    16         }
    17     }
    18     public String getName(){
    19         return this.name;
    20     }
    21     
    22     public void setHealth(int health){
    23         if(health < 0){
    24             System.out.println("健康值不合法.");
    25             this.health = 0;
    26         }else{
    27             this.health = health;
    28         }
    29     }
    30     public int getHealth(){
    31         return this.health;
    32     }
    33     
    34     public void setLove(int love){
    35         if(love < 0){
    36             System.out.println("亲密度不合法.");
    37             this.love = 0;
    38         }else{
    39             this.love = love;
    40         }
    41     }
    42     public int getLove(){
    43         return this.love;
    44     }
    45     
    46     public void setStrain(String strain){
    47         if(strain.equals("")){
    48             System.out.println("品种不能为空.");
    49         }else{
    50             this.strain = strain;
    51         }
    52     }
    53     public String getStrain(){
    54         return this.strain;
    55     }
    56     
    57     
    58     public Dog(){
    59         
    60     }
    61 
    62     public Dog(String name,int health,int love,String strain){
    63         this.setName(name);
    64         this.setHealth(health);
    65         this.setLove(love);
    66         this.setStrain(strain);
    67     }
    68     
    69     public void showInfo(){
    70         System.out.print("我的名字叫"+this.name);
    71         System.out.print(",健康值"+this.health);
    72         System.out.print(",亲密度"+this.love);
    73         System.out.println(",我是一只"+this.strain);
    74     }
    75 }

    6.this关键字(下)

    this表示对象本身。

    [1]this调用属性

    [2]this调用方法

    [3]this调用本类的构造方法。形式如下:

    1 this(arg1,arg2,...)

     通过下面练习加深理解

     1 public Dog(){
     2         
     3     }
     4     
     5     public Dog(String name,int health,int love){
     6         this.setName(name);
     7         this.setHealth(health);
     8         this.setLove(love);
     9     }
    10     
    11 
    12     public Dog(String name,int health,int love,String strain){
    13         //this.setName(name);
    14         //this.setHealth(health);
    15         //this.setLove(love);
    16         
    17         // this调用本类的其他构造方法
    18         // System.out.println("test");
    19         this(name,health,love);
    20         this.setStrain(strain);
    21         
    22         // showInfo();
    23         //this.showInfo();
    24     }

    注意:this调用其它构造方法时,只能放在构造方法的第一行

    7.静态常量

    在程序运行过程中,如果有一个量的值不发生改变,可以把该量声明成一个静态常量,用static final修饰。

  • 相关阅读:
    几款网络测试工具总结
    Linux安装telnet
    Linux下iptables 禁止端口和开放端口
    mysql创建某个数据库中的某张表 只读用户
    查看nginx版本号的几种方法
    Ngxtop-Nginx日志实时分析利器
    Nginx监控运维
    oracle经典书籍推荐
    华为典型局域网组网案例介绍(1)
    技术说明 路由器是如何工作的呢? 一个简单的解释
  • 原文地址:https://www.cnblogs.com/qq308015824/p/10742978.html
Copyright © 2011-2022 走看看