01 构造方法Constrector 02 构造方法的重载 03 给成员变量赋值的两种方法 04 学生类代码 05 手机类 06 长方形案例 08 static关键字 09 static的注意事项 10 静态变量和成员变量的区别 11 main方法的格式详细解释 12 工具类中使用静态
===
01 构造方法Constrector
public class Demo1_Constructor {
public static void main(String[] args) {
Person3 p = new Person3(); //在一创建对象的时候,系统就帮我调用了构造方法
//p.Person(); //构造方法不能用对象调用
p.show();
Person3 p2 = new Person3(); //再次创建对象
p2.show();
}
}
/*
* 构造方法概述和作用
* 给对象的数据(属性)进行初始化
* 构造方法格式特点
* 方法名与类名相同(大小也要与类名一致)
* 没有返回值类型,连void都没有
* 没有具体的返回值return
*/
class Person3 {
private String name;
private int age;
//构造方法
public Person3() {
//return; //构造方法也是有return语句的,格式是return;
name = "张三";
age = 23;
}
public void show() {
System.out.println(name + "..." + age);
}
}
===
02 构造方法的重载
/*
* 构造方法的重载:重载-方法名相同,与返回值类型无关(构造方法没有返回值),只看参数列表
* 如果没有给出构造方法,系统将自动提供一个无参构造方法
* 如果给出了构造方法,系统将不再提供默认的无参构造方法
*/
public class Demo2_Constructor {
public static void main(String[] args) {
Person4 p1 = new Person4();
p1.show();
System.out.println("--------------");
Person4 p2 = new Person4("张三",23);
p2.show();
}
}
class Person4 {
private String name; //姓名
private int age; //年龄
public Person4() { //空参构造
System.out.println("空参的构造");
}
public Person4(String name,int age) {//好处:可以动态的去给我们属性进行赋值
this.name = name;
this.age = age;
System.out.println("有参的构造");
}
public void show() {
System.out.println(name + "..." + age);
}
}
===
03 给成员变量赋值的两种方法
public class Demo5_Person {
public static void main(String[] args) {
Person5 p1 = new Person5("张三",23); //有参构造
p1 = new Person5("张四",23);//这种方式看运行结果貌似是改名了,其实是将原对象变成了垃圾
System.out.println(p1.getName() + "..." + p1.getAge());
System.out.println("------------------");
Person5 p2 = new Person5(); //空参构造创建对象
p2.setName("李四");
p2.setAge(24);
p2.setName("李五");
System.out.println(p2.getName() + "..." + p2.getAge());
}
}
/*
构造方法
给属性进行初始化
setXxx方法
修改属性值
这两种方式,在分开发中用setXxx更多一些,因为比骄傲 灵活
*/
class Person5 {
private String name;
private int age;
public Person5() { //空参构造
}
public Person5(String name,int age) { //有参构造
this.age = age;
this.name = name;
}
public void setName(String name) { //设置姓名
this.name = name;
}
public String getName() { //获取姓名
return name;
}
public void setAge(int age) { //设置年龄
this.age = age;
}
public int getAge() { //获取年龄
return age;
}
}
===
04 学生类代码
public class Demo4_Student {
public static void main(String[] args) {
Student4 s1 = new Student4(); //使用空参构造
s1.setName("张三"); //设置姓名
s1.setAge(23); //设置年龄
//不在本类中 要写成是 s1.getName() 在本类中可写成 name
System.out.println("我的姓名是:" + s1.getName() + "\t我的年龄是:" + s1.getAge());
//getXxx()获取属性值,也可以打印,也可以赋值给其他的变量,做其他的操作
Student4 s2 = new Student4("李四",24); //使用有参构造
s2.show(); //只是为了显示属性值
}
}
/*
学生类:
成员变量:name,age
构造方法:无参,带两个参数
成员方法:getXxx()/setXxx() show():输出该类的所有成员变量值
给成员变量赋值:
setXxx()方法
构造方法
输出成员变量值的方式:
通过getXxx()分别获取然后拼接
通过调用show()方法搞定
*/
class Student4 {
private String name;
private int age;
public Student4() { //空参构造
}
public Student4(String name,int age) { //有参构造
this.name = name;
this.age =age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void show() {
System.out.println("我的姓名是:" + name + "\t我的年龄是:" + age);
}
}
===
05 手机类
public class Demo5_Phone {
public static void main(String[] args) {
Phone5 p1 = new Phone5(); //无参构造
p1.setBrand("ipone-X");
p1.setPrice(9875);
System.out.println("手机品牌:" + p1.getBrand() + "\t价格:" + p1.getPrice());
Phone5 p2 = new Phone5("Huawei",2300);//有参构造
p2.show();
}
}
/*
手机类:
成员变量:品牌brand,价格price
构造方法:无参,有参
成员方法:setXxx和getXxx show
*/
class Phone5 {
private String brand;
private int price;
public Phone5() { //无参构造
}
public Phone5(String brand,int price) {
this.setBrand(brand);
this.price = 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 void show() {
System.out.println("手机品牌:" + brand + "\t价格:" + price);
}
}
===
06 长方形案例
public class Test1_Rectangle { //Rectangle 矩形
public static void main(String[] args) {
Rectangle r1 = new Rectangle(); //无参构造
r1.setWidth(5);
r1.setHigh(4);
System.out.println("矩形周长为:" + 2*(r1.getWidth()+r1.getHigh()));
System.out.println("矩形面积为:" + (r1.getWidth()*r1.getHigh()));
Rectangle r2 = new Rectangle(5,4); //有参构造
r2.getLength();
r2.getArea();
}
}
/*
需求:定义一个长方形类,定义 求周长和面积的方法
然后定义一个测试类进行测试
分析:成员变量:宽width,高high
空参有参构造方法
成员方法:setXxx和getXxx 求周长:getLength() 求面积:getArea()
*/
class Rectangle {
private int width;
private int high;
public Rectangle() { //无参构造
}
public Rectangle(int width,int high) { //有参构造
this.width = width;
this.high = high;
}
public void setWidth(int width) { //设置宽度
this.width = width;
}
public int getWidth() { //获取宽度
return width;
}
public void setHigh(int high) { //设置高度
this.high = high;
}
public int getHigh() { //获取高度
return high;
}
public void getLength() { //求矩形周长
System.out.println("矩形周长为:" + 2*(width+high) );
}
public void getArea() { //求矩形面积
System.out.println("矩形面积为:" + (width*high));
}
}
===
07 员工案例练习
public class Test2_Employee { //employee员工
public static void main(String[] args) {
Employee e1 = new Employee();//无参构造
e1.setName("张三");
e1.setId("123456");
e1.setSalary(10000);
e1.work();
Employee e2 = new Employee("李四","123455",9999.99);//有参构造
e2.work();
}
}
/*
需求:定义一个员工类Employee
自己分析出几个成员,然后给出成员变量
姓名name,工号id,工资salary
构造方法:空参和有参
getXxx()和setXxx方法
以及一个显示所有成员信息的方法,并测试
work()
*/
class Employee {
private String name; //姓名
private String id; //工号
private double salary; //工资
public Employee() { //无参构造
}
public Employee(String name,String id,double salary) {//有参构造
this.name = name;
this.id = id;
this.salary = salary;
}
public void setName(String name) { //设置姓名
this.name = name;
}
public String getName() { //获取姓名
return name;
}
public void setId(String id) { //设置工号
this.id = id;
}
public String getId() { //获取工号
return id;
}
public void setSalary(double salary) {//设置工资
this.salary = salary;
}
public double getSalary() { //获取工资
return salary;
}
public void work() {
System.out.println("姓名:" + name + "\n工号:" + id + "\n工资:" + salary);
}
}
===
08 static关键字
A:static关键字的特点
a:随着类的加载而加载
b:优先于对象存在
c:被类的所有对象共享
举例:咱们班级的同学学生应该共用同一个班级编号
其实这个特点也是在告诉我们什么时候使用静态
如果某个成员变量是被所有对象共享的,那么它就应该定义为静态的
举例:饮水机(静态修饰)和水杯(不能用静态修饰)
d:可以通过类名调用
其实它本身也可以通过对象4其为调用
推荐使用类名调用
静态修饰的内容一般我们称其为:与类相关的,类成员
===
09 static的注意事项
public class Demo1_Static {
public static void main(String[] args) {
// Demo d1 = new Demo();
// d1.print1();
Demo.print2();
}
}
/*
A:static的注意事项
a:再静态方法中是没有this关键字的
理解:静态是随着类的加载而加载,this是随着对象的创建而存在
静态比对象先存在
b:静态方法只能访问静态的成员变量和静态的成员方法
静态方法:
成员变量:只能访问静态变量
成员方法:只能访问静态成员方法
非静态方法:
成员变量:可以是静态的,也可以是非静态的
成员方法:可是是静态的成员方法,也可以是非静态的成员方法
静态只能访问静态
*/
class Demo {
int num1 = 10; //非静态的成员变量
static int num2 = 20; //静态的成员变量
// public void print1() { //非静态的成员方法,既可以访问静态的成员也可以访问非静态的
// System.out.println(num1);
// System.out.println(num2);
// }
public static void print2() { //静态的成员方法
//System.out.println(num1);
//静态的成员方法不能访问非静态的,
//Exception in thread "main" java.lang.Error: 无法解析的编译问题:不能对非静态字段 num1 进行静态引用
System.out.println(num2);
}
}
10 静态变量和成员变量的区别
静态变量也叫类变量 成员变量也叫对象变量
A:所属不同
静态变量属于类,所以也称为类变量
成员变量属于对象,所以也成为实例变量(对象变量)
B:内存中位置不同
静态变量存储与方法区内的静态区
成员变量存储与堆内存
C:内存出现时间不同
静态变量随着类的加载而加载,随着类的消失而消失
成员变量随着对象的创建而存在,随着对象的消失而消失
D:调用不同
静态变量可以通过类名调用,也可以通过对象调用
成员变量只能通过对象名调用
11 main方法的格式详细解释
public class Demo_Main {
public static void main() {
/*
public : 被jvm调用,所以权限要足够大
static : 被jvm调用,不需要创建对象,直接类名.调用即可
void : 被jvm调用,不需要有任何的返回值
main : 只有这样写才能被jvm识别,main不是关键字
String[] args : 以前是用来接收键盘录入的
*/
}
===
12 工具类中使用静态
public class Demo1_ArrayTool {
public static void main(String[] args) {
int[] arr = {33,22,34,55,66};
/*ArrayTool at = new ArrayTool();
int max = at.getMax(arr);
System.out.println(max); //获取最值
System.out.println("----------------");
at.print(arr); //打印
System.out.println();
System.out.println("----------------");
System.out.println("反转后:");
at.revArray(arr); //反转
at.print(arr);*/
ArrayTool.print(arr);
}
}
//另一个class
class ArrayTool {
//如果一个类中所有的方法都是静态的,需要多做一步,目的是不让其他类船舰本类对象
//直接用类名.调用即可
private ArrayTool() {}//私有构造方法
//1,获取最大值
public static int getMax(int[] arr) {
int max = arr[0]; //记录第一个元素
for(int i = 1;i < arr.length ; i++){ //从第二个元素开始遍历
if(max < arr[i]){ //max与数组中其他元素比较
max = arr[i]; //记录住较大的
} //将最大值返回
}
return max;
}
//2,数组的遍历
public static void print(int[] arr) {
for(int i = 0;i < arr.length ; i++) { //遍历数组
System.out.print(arr[i] + " ");
}
}
//3,数组的反转
public static void revArray(int[] arr) {
for (int i = 0;i < arr.length / 2;i++){ //循环次数是元素个数的一半
/*
arr[0]与arr[arr.length-1-0] 交换
arr[1]与arr[arr.length-1-1] 交换
arr[2]与arr[arr.length-1-2] 交换
*/
int temp = arr[i];
arr[i] = arr[arr.length-1-i];
arr[arr.length-1-i] = temp;
}
}
}