Java advanced object-oriented

1: The difference between member variables and local variables

(1)在类中的位置不同
	成员变量:类中方法外
	局部变量:方法定义中或者方法声明上
(2)在内存中的位置不同
	成员变量:在堆中
	局部变量:在栈中
(3)生命周期不同
	成员变量:随着对象的创建而存在,随着对象的消失而消失
	局部变量:随着方法的调用而存在,随着方法的调用完毕而消失
(4)初始化值不同
	成员变量:有默认值
	局部变量:没有默认值,必须定义,赋值,然后才能使用
class Varialbe {
    
    
	//成员变量
	//int num = 10;
	int num; //0
	
	public void show() {
    
    
		//int num2 = 20; //局部变量
		//可能尚未初始化变量num2
		//int num2; //没有默认值
		int num2 = 20;
		System.out.println(num2);
		
		//int num = 100;
		System.out.println(num);
	}
}

2: The problem of class as a formal parameter?

如果你看到一个方法需要的参数是一个类名,就应该知道这里实际需要的是一个具体的对象。
形式参数的问题:
	基本类型:形式参数的改变不影响实际参数
	引用类型:形式参数的改变直接影响实际参数
class Demo {
    
    
	public int sum(int a,int b) {
    
    
		return a + b;
	}
}

//形式参数是引用类型
class Student {
    
    
	public void show() {
    
    
		System.out.println("我爱学习");
	}
}

class StudentDemo {
    
    
	//如果你看到了一个方法的形式参数是一个类类型(引用类型),这里其实需要的是该类的对象。
	public void method(Student s) {
    
     //调用的时候,把main方法中的s的地址传递到了这里 Student s = new Student();
		s.show();
	}
}

class ArgsTest {
    
    
	public static void main(String[] args) {
    
    
		//形式参数是基本类型的调用
		Demo d = new Demo();
		int result = d.sum(10,20);
		System.out.println("result:"+result);
		System.out.println("--------------");
		
		//形式参数是引用类型的调用
		//需求:我要调用StudentDemo类中的method()方法
		StudentDemo sd = new StudentDemo();
		//创建学生对象
		Student s = new Student();
		sd.method(s); //把s的地址给到了这里
	}
}

3: Anonymous object

(1)没有名字的对象
(2)应用场景
	A:调用方法,仅仅只调用一次的时候。不适合多次调用的时候
	B:可以作为实际参数传递。
	好处:匿名对象调用完毕就是垃圾。可以被垃圾回收器回收。
class Student {
    
    
	public void show() {
    
    
		System.out.println("我爱学习");
	}
}

class StudentDemo {
    
    
	public void method(Student s) {
    
    
		s.show();
	}
}

class NoNameDemo {
    
    
	public static void main(String[] args) {
    
    
		//带名字的调用
		Student s = new Student();
		s.show();
		s.show();
		System.out.println("--------------");
		
		//匿名对象
		//new Student();
		//匿名对象调用方法
		new Student().show();
		new Student().show(); //这里其实是重新创建了一个新的对象
		System.out.println("--------------");
		
		
		//匿名对象作为实际参数传递
		StudentDemo sd = new StudentDemo();
		//Student ss = new Student();
		//sd.method(ss); //这里的s是一个实际参数
		//匿名对象
		sd.method(new Student());
		
		//在来一个
		new StudentDemo().method(new Student());
 	}
}

4: Package

(1)隐藏实现细节,提供公共的访问方式
(2)好处:
	A:隐藏实现细节,提供公共的访问方式
	B:提高代码的复用性
	C:提高代码的安全性
(3)设计原则
	把不想让外界知道的实现细节给隐藏起来,提供公共的访问方式
(4)private是封装的一种体现。
	封装:类,方法,private修饰成员变量

5: private keyword

(1)私有的意义,可以修饰成员变量和成员方法
(2)特点:
	被private修饰的后的成员只能在本类中被访问
(3)private的应用:
	以后再写一个类的时候:
		把所有的成员变量给private了
		提供对应的getXxx()/setXxx()方法
//定义学生类
class Student {
    
    
	//姓名
	private String name;
	//年龄
	private int age;
	
	//姓名获取值
	public String getName() {
    
    
		return name;
	}
	
	//姓名设置值
	public void setName(String n) {
    
    
		name = n;
	}
	
	//年龄获取值
	public int getAge() {
    
    
		return age;
	}
	
	//年龄赋值
	public void setAge(int a) {
    
    
		age = a;
	}
}

//测试类
class StudentTest {
    
    
	public static void main(String[] args) {
    
    
		//创建学生对象
		Student s = new Student();
		
		//使用成员变量
		//错误:被私有修饰了,外界不能直接访问了
		//System.out.println(s.name+"---"+s.age);
		System.out.println(s.getName()+"---"+s.getAge());
		
		//给成员变量赋值
		//s.name = "林青霞";
		//s.age = 27;
		//通过方法给赋值
		s.setName("林青霞");
		s.setAge(27);
		System.out.println(s.getName()+"---"+s.getAge());
	}
}

6: this keyword

(1)代表当前类的引用对象
	记住:哪个对象调用方法,该方法内部的this就代表那个对象
(2)this的应用场景:
	A:解决了局部变量隐藏成员变量的问题
	B:其实this还有其他的应用,以后再说。
开发原则:起名时做到见名知意

	public String getName() {
    
    
		return name; //这里其实是隐含了this
	}
	
	public void setName(String name) {
    
    
		this.name = name;
	}
	
	public int getAge() {
    
    
		return age;
	}
	
	public void setAge(int age) {
    
    
		this.age = age;
	}

7: Construction method

(1)作用:用于对对象的数据进行初始化
(2)格式:
	A:方法名和类名相同
	B:没有返回值类型,连void都不能有
	C:没有返回值
	
	思考题:构造方法中可不可以有return语句呢?
	可以。而是我们写成这个样子就OK了:return;
	其实,在任何的void类型的方法的最后你都可以写上:return;
	
(3)构造方法的注意事项
	A:如果我们没写构造方法,系统将提供一个默认的无参构造方法
	B:如果我们给出了构造方法,系统将不再提供默认构造方法
		如果这个时候,我们要使用无参构造方法,就必须自己给出。
		推荐:永远手动自己给出无参构造方法。
(4)给成员变量赋值的方式
	A:setXxx()
	B:带参构造方法
(5)标准案例
	class Student {
    
    
			private String name;
			private int age;
			
			public Student(){
    
    }
			
			public Student(String name,int age) {
    
    
				this.name = name;
				this.age = age;
			}
			
			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;
			}
		}
		
		测试:
		class StudentDemo {
    
    
			public static void main(String[] args) {
    
    
				//方式1
				Student s1 = new Student();
				s1.setName("林青霞");
				s1.setAge(27);
				System.out.println(s1.getName()+"---"+s1.getAge());
				
				//方式2
				Student s2 = new Student("刘意",30);
				System.out.println(s2.getName()+"---"+s2.getAge());
			}
		}
以后再提类的组成:
		成员变量
		构造方法
		成员方法
			根据返回值:
				void类型
				非void类型
			形式参数:
				空参方法
				非空参方法

8: Code: What did Student s = new Student(); do?

(1)把Student.class文件加载到内存
(2)在栈内存为s开辟空间
(3)在堆内存为学生对象申请空间
(4)给学生的成员变量进行默认初始化。null,0
(5)给学生的成员变量进行显示初始化。林青霞,27
(6)通过构造方法给成员变量进行初始化。刘意,30
(7)对象构造完毕,把地址赋值给s变量

9: Object-oriented exercises

(1)标准的手机类的定义和测试
(2)Demo类有求和方法,Test类进行测试。
	什么时候定义成员变量?
	当该变量是用来描述一个类的时候。
(3)长方形案例
(4)员工案例
(5)MyMath案例(自己提供加减乘除并测试)
  • When is a variable defined as a member variable:
    If this variable is used to describe the information of this class, then the variable should be defined as a member variable.

  • Where exactly is the variable defined?
    The smaller the scope of the variable, the better. Because it can be recycled in time

10: static keyword

(1)静态的意思。可以修饰成员变量和成员方法。
(2)静态的特点:
	A:随着类的加载而加载
	B:优先于对象存在
	C:被类的所有对象共享
		这其实也是我们判断该不该使用静态的依据。
		举例:饮水机和水杯的问题思考
	D:可以通过类名调用
		既可以通过对象名调用,也可以通过类名调用,建议通过类名调用。
(3)静态的内存图
	静态的内容在方法区的静态区
(4)静态的注意事项;
	A:在静态方法中没有this对象
	B:静态只能访问静态(代码测试过)
(5)静态变量和成员变量的区别
	A:所属不同
		静态变量:属于类,类变量
		成员变量:属于对象,对象变量,实例变量
	B:内存位置不同
		静态变量:方法区的静态区
		成员变量:堆内存
	C:生命周期不同
		静态变量:静态变量是随着类的加载而加载,随着类的消失而消失
		成员变量:成员变量是随着对象的创建而存在,随着对象的消失而消失
	D:调用不同
		静态变量:可以通过对象名调用,也可以通过类名调用
		成员变量:只能通过对象名调用
(6)main方法是静态的
	public:权限最大
	static:不用创建对象调用
	void:返回值给jvm没有意义
	main:就是一个常见的名称。
	String[] args:可以接收数据,提供程序的灵活性
		格式:java MainDemo hello world java
			  java MainDemo 10 20 30
/区分静态成员变量和非静态成员变量
public class Book {
    
    
    private int price;
    static private String chubanshe;

    public Book(){
    
    }

    public Book(int price,String chubanshe){
    
    
        this.price=price;
        this.chubanshe=chubanshe;
    }

    public int Getprice(){
    
    
        return this.price;
    }

    public String Getchubanshe(){
    
    
        return this.chubanshe;
    }

}
/(String name,String author,String chubanshe,double price)
//共享变量,一个改变,全部改变
public class Booktest {
    
    
    public static void main(String[] args) {
    
    
        Book a=new Book(20,"安徽大学出版社");
        System.out.println(a.Getchubanshe());
        System.out.println(a.Getprice());
        Book b=new Book(30,"华北师范");
        System.out.println(a.Getchubanshe());
        System.out.println(a.Getprice());
        System.out.println(b.Getprice());
        System.out.println(b.Getchubanshe());
        Book c=new Book(50,"南京大学");
        System.out.println(a.Getchubanshe());
        System.out.println(a.Getprice());
        System.out.println(b.Getprice());
        System.out.println(b.Getchubanshe());
        System.out.println(c.Getchubanshe());
        System.out.println(c.Getprice());
        Book d=new Book();
        System.out.println(a.Getchubanshe());
        System.out.println(a.Getprice());
        System.out.println(b.Getprice());
        System.out.println(b.Getchubanshe());
        System.out.println(c.Getchubanshe());
        System.out.println(c.Getprice());
        System.out.println(d.Getprice());
        System.out.println(b.Getchubanshe());
    }

}

Insert picture description here

Case

Definition and testing of standard mobile phones

public class Phone {
    
    
    private int price;
    private String brand;
    private int number;

    public Phone(){
    
    }

    public Phone(int price,String brand,int num){
    
    
        this.brand=brand;
        this.Setprice(price);
        this.Setnumber(num);

    }

    public int Getprice(){
    
    
        return this.price;
    }

    public String Getbrand(){
    
    
        return this.brand;
    }

    public int Getnumber(){
    
    
        return this.number;
    }

    public void Setprice(int price){
    
    
        if(price<0){
    
    
            System.out.println("输入数据不合法");
        }
        else{
    
    
            this.price=price;
        }
    }

    public void Setbrand(String brand){
    
    
        this.brand=brand;
    }

    public void Setnumber(int number){
    
    
        if(number<0){
    
    
            System.out.println("输入的数据不合法");
        }
        else{
    
    
            this.number=number;
        }
    }

    public String toString(){
    
    
        return "price:"+this.price+'\n'
                +"brand:"+this.brand+'\n'
                +"number"+this.number;
    }
}
public class Phonetest {
    
    
    public static void main(String[] args) {
    
    
        Phone p1=new Phone(1900,"诺基亚",50);
        System.out.println(p1.toString());
        Phone p2=new Phone();
        p2.Setbrand("三星");
        p2.Setnumber(58);
        p2.Setprice(-10);
        System.out.println(p2.toString());
    }

}

Insert picture description here
The Demo class has a sum method, and the Test class performs testing.

package cn.shujia.day6;
public class Demo {
    
    
    public Demo(){
    
    }

    public int sum(int a,int b){
    
    //a,b 定义为局部变量
        return a+b;
    }

}
public class Test {
    
    
    public static void main(String[] args) {
    
    
        Demo d=new Demo();
        System.out.println(d.sum(40,21));
    }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45798550/article/details/107971243