Java学习的第十二天(JavaSE精英篇02_构造方法_static关键字)

一、构造方法

格式:

public 类名(){    //构造方法的方法名--类名()  没有返回值,连void都不能有
	
}

作用:1.用来创建对象用的 2.给属性进行赋值

二、无参构造和有参构造
//无参构造方法
package com.bianyiit.cast;

public class Car {  //这个Car是类名
//属性,记住为了避免用户乱赋值用private修饰,使用set和get方法对私有变量进行赋值
    private String pingpai;
    private int price;
    private String color;

    public Car() {  //这个Car是无参构造方法的名字,和类名相等
    }
    
    public String getPingpai() {
        return pingpai;
    }

    public void setPingpai(String pingpai) {
        this.pingpai = pingpai;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

//测试类文件CarTest.java
package com.bianyiit.cast;

public class CarTest {
    public static void main(String[] args) {
        Car car1 = new Car();
        System.out.println(car.getPingpai()+car.getPrice()+car.getColor());
    }
}
//输出结果:null0null

总结:
1.构造方法是用来创建对象用的--这里默认创建的对象时car
2.构造方法是通过new关键字+构造方法名()来调用的
3.如果类中没有写无参构造,那么创建对象的时候会默认调用无参构造

//有参构造方法
package com.bianyiit.cast;

public class Car {  //这个Car是类名
//属性,记住为了避免用户乱赋值用private修饰,使用set和get方法对私有变量进行赋值
    private String pingpai;
    private int price;
    private String color;

    public Car() {  //这个Car是无参构造方法的名字,和类名相等
    }
    public Car(String pingpai, int price, String color) {   //这个Car是有参构造方法的名字,和类名相等
    //有参构造除了创建对象,还能够给属性赋值
        this.pingpai = pingpai;
        this.price = price;
        this.color = color;
    }
    public String getPingpai() {
        return pingpai;
    }

    public void setPingpai(String pingpai) {
        this.pingpai = pingpai;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

//测试类文件CarTest.java
package com.bianyiit.cast;

public class CarTest {
    public static void main(String[] args) {
        Car car1 = new Car();
        System.out.println(car.getPingpai()+car.getPrice()+car.getColor());
        Car car1 = new Car("奔驰", 200, "黑色");
        System.out.println(car.getPingpai()+car.getPrice()+car.getColor());
    }
}
//输出结果:
		null0null
		奔驰200黑色

当存在有参构造方法的时候,就不能默认去调用无参构造方法了,建议创建一个类的时候,应该把无参和有参的构造方法都写上

构造方法也是方法的重载

三、什么是重载??
在同一个类中,方法名相同,参数列表不同(参数的个数,参数的类型,参数的顺序),与返回值无关
四、给属性赋值的方式有哪些?
1.通过setXxx()给属性赋值(注意:属性必须先私有化)和getXxx()取值
	例如: s1.setName("张三");
          String name=s1.getName();
          System.out.println(name);
2.通过有参构造方法给属性赋值
	例如: Car car1 = new Car("奔驰", 200, "黑色");
五、如何编写一个完整的类

1.属性要私有化
2.要有setXxx()和getXxx()去设置属性和获取属性值
3.要有无参和有参构造方法
4.要重写toString()
5.要重写equals()和hashcode()
6.要序列化

六、快捷键快速生成各种方法

1.先通过alt+insert打开Generate列表

2.点击Constructor(构造器)用来生成有参和无参的构造方法

3.点击SelectNone添加无参构造方法,按住ctrl+a全选点击ok添加有参构造方法
4.回到步骤1,然后点击Getter和Setter方法,点击ok,然后按住ctrl+a全选点击ok添加get和set方法

5.回到步骤1,然后点击toString方法,点击ok,然后按住ctrl+a全选点击ok重写toString方法(为了避免堆内存泄漏)

七、this关键字:this是当前对象的引用
this关键字:
1.区分局部变量和成员变量。
2.谁调用方法,this代表的就是谁   this是当前对象的引用。 
八、构造方法里面调用其他构造方法总结
	//有参构造1
    public Student(){
        this("张三",18);     //通过this关键字调用
    }
    //有参构造2
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
  
	//自定义方法
    public void study(){
        this.eat();   --通过this关键字调用自定义方法
        System.out.println("好好学习,天天向上");
        System.out.println(this.name);
    }
    public void eat(){

        System.out.println("吃饭.....");
    }
1.需要使用this()进行调用
2.构造方法里面只能调用一次其他的构造方法
3.this(参数) 只能放在构造方法里面的有效代码的第一行。
九、Static(静态的)特定
package com.bianyiit.cast;

public class Person {
public   String  name;  
public    int age;
public    char sex;
public    static String   contry;//所有的对象共享的资源,通过static修饰

    public Person() {   //无参构造方法
    }

    public Person(String name, int age, char sex,String contry) {   //有参构造方法
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.contry=contry;
    }
    //set和get方法
    public String getContry() {
        return name;
    }

    public void setContry(String contry) {
        this.contry = contry;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }
    //重写toString()方法,避免堆内存泄漏!
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +", sex=" + contry+
                '}';
    }

}

package com.bianyiit.cast;

public class PersonTest {
    public static void main(String[] args) {
        //创建四个对象
        Person p1 = new Person("张三", 20, '男',"中国");
        Person p2 = new Person("李四", 21, '女',"中国");
        Person p3 = new Person("王五", 22, '妖',"中国");
        Person p4 = new Person("赵六", 23, '女',"中国");
        System.out.println(p1);
        //每个对象都有自己特有的三个属性(属性值)
        //把所有的对象的共享资源  抽取出来-----需要用static修饰
        //static(静态的)所修饰的成员变量是所有的对象共享的资源。
        //static既可以修饰成员变量又可以修饰成员方法。修饰的成员变量称之为静态变量,修饰的方法称之为静态方法。
如何使用 static修饰的成员属性?
        //idea  调用静态成员时没有提示功能。
        //方式1.通过对象名调用属性(不推荐使用)
        String contry = p1.contry;
        System.out.println(contry);
        //方式2.通过类名直接调用(推荐使用)
        String contry1 = Person.contry;
        System.out.println(contry1);
        p1.contry="美国";
        p1.name="隔壁老王";
        String contry2 = p2.contry;
        System.out.println(contry2);//由于contry是共享资源,当p1对这个资源进行修改时,通过p2打印出来的contry也会从中国-->美国
        String name = p2.name;
        System.out.println("p2的名字:"+name);//但是名字没有使用static修饰,属于私有变量,当p1对名字进行更改不会影响p2输出的结果
    }
}
为什么推荐使用类名调用static修饰的成员?

static修饰的成员加载到内存优先于对象的创建

static修饰的变量在内存中的分布图解

十、成员变量和静态变量的区别
1.生命周期不同
	成员变量随着对象的创建而存在,随着对象的被回收而释放
	静态变量随着类的加载而存在,随着类的消失而消失
	变量要存储的数据量大的时候建议不要使用static,因为使用static修饰会一直占用方法区中的空间,如果是在堆里面,垃圾回收机制会自动回收
	局部变量是方法执行的时候会在栈内存中分配内存空间,方法执行完之后释放空间
2.调用的方式不同
	成员变量只能被对象引用
	静态变量可以被对象调用,还可以被类名调用
3.别名不同
	由于创建对象的过程也被称为实例化,所以成员变量也被称为实例变量
	静态变量随着类的加载而存在,所以静态变量也被称为类变量
4.数据的存储位置不同
	成员变量存储在堆内存的对象中,所以也叫对象的特有数据
	静态变量数据存储在方法的静态区
	局部变量存储在栈内存中
十一、静态方法使用的注意事项
1.对象可以调用静态和非静态的成员(成员变量和成员方法),类名只能调用静态方法
2.静态方法中不可以使用this,super关键字
3.主函数是静态的
注意:main是函数名,不是关键字,只是一个JVM识别的固定的名字而已
十二、静态方法中不可以使用非静态成员,why?

因为静态修饰的成员是由类的加载而加载到内存,优先于对象的存在,而非静态变量是随着对象的创建而加载到内存的,所以静态方法调用非静态成员的时候,非静态成员在内存中都还没有存在,所以不行!

课堂练习
//Car.java
package com.bianyiit.cast;

public class Car {
    private String pingpai;
    private int price;
    private String color;

    public Car() {
    }

    public Car(String pingpai, int price, String color) {
        this.pingpai = pingpai;
        this.price = price;
        this.color = color;
    }

    public String getPingpai() {
        return pingpai;
    }

    public void setPingpai(String pingpai) {
        this.pingpai = pingpai;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Car{" +
                "pingpai='" + pingpai + '\'' +
                ", price=" + price +
                ", color='" + color + '\'' +
                '}';
    }
    public void compare(Car car){
        if(this.price==car.getPrice()){
            System.out.println(pingpai+"的价格与"+car.getPingpai()+"的价格相等");
        }else{
            System.out.println(pingpai+"的价格与"+car.getPingpai()+"的价格不相等");
        }
    }
}

//CarTest.java
package com.bianyiit.cast;

public class CarTest {
    public static void main(String[] args) {
        Car car = new Car();
        Car car1 = new Car("奔驰", 200, "黑色");
        Car car2 = new Car("劳斯莱斯", 150, "红色");
        System.out.println(car1);
        System.out.println(car2);
        car1.compare(car2);
    }
}
//输出结果:
	Car{pingpai='奔驰', price=200, color='黑色'}
	Car{pingpai='劳斯莱斯', price=150, color='红色'}
	奔驰的价格与劳斯莱斯的价格不相等
//Method_GouZao.java
package com.bianyiit.cast;

public class Method_GouZao {
   private String name;
   private int age;
   private char sex;
    //无参构造方法
    public Method_GouZao(){

    }
    //有参构造1
    public Method_GouZao(String name){
        this.name=name;
    }
    //有参构造2
    public Method_GouZao(String name,int age){
        this.name=name;
        this.age=age;
    }
    //有参构造3
    public Method_GouZao(String name,int age,char sex){
        this.name=name;
        this.age=age;
        this.sex=sex;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }
    public String toString(){
        return "name:"+name+"\nage:"+age+"\nsex:"+sex;
    }
}

//GouZaoTest.java
package com.bianyiit.cast;

public class GouZaoTest {
    public static void main(String[] args) {
        Method_GouZao gouzao1 = new Method_GouZao();
        System.out.println(gouzao1);
        System.out.println();
        Method_GouZao gouzao2 = new Method_GouZao("张三");
        System.out.println(gouzao2);
        System.out.println();
        Method_GouZao gouzao3 = new Method_GouZao("张三", 18);
        System.out.println(gouzao3);
        System.out.println();
        Method_GouZao gouzao4 = new Method_GouZao("张三", 18, '男');
        System.out.println(gouzao4);
        System.out.println();

    }
}
//输出结果:
	name:null
	age:0
	sex: 
	
	name:张三
	age:0
	sex: 
	
	name:张三
	age:18
	sex: 
	
	name:张三
	age:18
	sex:男
//Person_static.java
package com.bianyiit.cast;

public class Person_static {
   private String name;
   private int age;
   private char sex;
   static String country;


    public Person_static() {
    }

    public Person_static(String name, int age, char sex,String country) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.country=country;
    }


}
//PersonTest_static.java
package com.bianyiit.cast;

public class PersonTest_static {
    public static void main(String[] args) {
        Person_static p1 = new Person_static("张三", 18, '男',"中国");
        Person_static p2 = new Person_static("李四", 19, '女',"中国");
        Person_static p3 = new Person_static("王五", 20, '妖',"中国");
        Person_static p4 = new Person_static("赵六", 21, '仙',"中国");
        String country=Person_static.country;
        System.out.println(country);
        Person_static.country="美国";
        System.out.println(p4.country);
        p1.country="修仙界";  //不推荐使用
        System.out.println(Person_static.country);
    }
}
//输出结果:
	中国
	美国
	修仙界
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值