构造方法
1、构造方法概述和格式
(1)概述:给对象的数据(属性)进行初始化
(2)格式特点
- 方法名与类名相同(大小写也要与类名一致)
- 没有返回值类型,连void都没有
- 没有具体的返回值return;
public static void main(String[] args) { Phone p = new Phone(); } } class Phone{ private String brand; private int price; //构造方法 public Phone(){ System.out.println("啦啦啦"); return; } }
2、构造方法的重载
(1)概述
方法名相同,与返回值类型无关(构造方法没有返回值),只看参数列表
(2)注意事项
- 如果没有给出构造方法,系统将自动提供一个构造方法
- 如果给了构造方法,系统将不再提供构造方法
- 所以最好一直写出无参构造方法
public static void main(String[] args) { Phone p1 = new Phone(); p1.show(); System.out.println("----------------"); Phone p2 = new Phone("vivoxplay5",2999); p2.show(); } } class Phone{ private String brand; //品牌 private int price; //价格 //无参构造方法 public Phone(){ System.out.println("无参构造"); } //有参构造器 public Phone(String brand, int price) { this.brand = brand; this.price = price; System.out.println("有参构造"); } public void show(){ System.out.println(brand+"..."+price); } }
3、给成员变量赋值的两种方式的区别
/** * 无参构造方法:给属性进行初始化 * set/get方法:修改属性值 * @param args */ public static void main(String[] args) { //无参构造创建对象 Phone p1 = new Phone(); p1.setBrand("vivoxplay5"); p1.setPrice(2999); p1.setBrand("vivo"); p1.show(); System.out.println("----------------"); //有参构造创建对象 Phone p2 = new Phone("vivoxplay5",2999); //这种方式并不是改名,而是将原对象变成垃圾 //p2 = new Phone("vivo",2000); p2.show(); } } class Phone{ private String brand; //品牌 private int price; //价格 public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } //无参构造方法 public Phone(){ System.out.println("无参构造"); } //有参构造器 public Phone(String brand, int price) { this.brand = brand; this.price = price; System.out.println("有参构造"); } public void show(){ System.out.println(brand+"..."+price); } }
4、长方形案例
public static void main(String[] args) { 长方形 c = new 长方形(4,6); System.out.println(c.周长()); System.out.println(c.面积()); } } /** * 定义一个长方形类,求周长和面积的方法 * 然后定义一个测试类进行测试 * */ class 长方形{ private int a; private int b; public 长方形() { //无参构造 } public 长方形(int a, int b) { //有参构造 super(); this.a = a; this.b = b; } public int getA() { return a; } public void setA(int a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } public int 周长(){ return (a+b)*2; } public int 面积(){ return a*b; } }
5、static关键字
特点:
(1)随着类的加载而加载
(2)优先于对象存在
(3)被类的所有对象共享
- 举例:班上的同学应该共用同一个班级编号
- 如果成员变量是被所有对象共享的,那么它就应该定义为静态的
- 举例:饮水机(用静态修饰)
- 水杯(不能用静态修饰)
- 共性用静态,特性用非静态
(4)可以通过类名调用
- 它本身也可以通过对象名调用
- 推荐使用类名调用
- 静态修饰的内容一般称为:与类相关的,类成员
注意事项:
(1)在静态方法中式没有this关键字的
- 静态是随着类的加载而加载,this是随着对象的创建而存在的
- 静态比对象先存在
(2)静态方法只能访问静态的成员变量和静态的成员方法
public static void main(String[] args) { c c1 = new c(); c1.print(); c.print1(); } } class c{ int num1=10; //非静态成员变量 static int num2 = 20; //静态成员变量 public void print(){ //非静态成员方法,既可以访问静态的,也可以访问非静态的 System.out.println(num1); System.out.println(num2); } public static void print1(){ //静态成员方法 //System.out.println(num1); //静态成员方法不能访问非静态的 System.out.println(num2); } }
6、静态变量和成员变量的区别
(1)所属不同
静态变量属于类,所以也称为类变量
成员变量属于对象,所以也称为实例变量(对象变量)
(2)内存中位置不同
静态变量存储与方法区的静态区
成员变量存储于堆内存
(3)内存出现时间不同
静态变量随着类的加载而加载,随着类的消失而消失
成员变量随着对象的创建而存在,随着对象的消失而消失
(4)调用不同
静态变量可以通过类名调用,也可以通过对象调用
成员变量只能通过对象名调用
7、Math类的随机数功能
public static void main(String[] args) { double d = Math.random(); //生成一个随机小数 System.out.println(d); //生成1-100的随机数 int a = (int)(Math.random()*100+1); System.out.println(a); } }