小白学习java第8天

1. 封装之private关键字

  • 概述

    • 使用private关键字修饰属性和行为,那么该属性的行为只能在本类中访问
  • 代码

    Human类

    
    public class Human {
    	private String name;
    	private int age;
    	
    	
    	public void setName(String str) {
    		name = str;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	
    	public void setAge(int str) {
    		age = str;
    	}
    	
    	public int getAge() {
    		return age;
    	}
    
    }
    
    

    Demo01

    public class Demo01 {
    	public static void main(String[] args) {
    		Human human = new Human();
    		human.setName("海风");
    		human.setAge(18);
    		
    		System.out.println(human.getAge());
    		System.out.println(human.getName());
    	}
    }
    

2. this关键字

  • 概述

    • this就是当前对象
  • 代码

    
    public class Human2 {
    	private String name;
    	private int age;
    	
    	
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	
    	public void setAge(int age) {
    		if(age >= 0 && age <= 130) {
    			this.age = age;
    		}else {
    			System.out.println("输入错误");
    		}
    	}
    	
    	public int getAge() {
    		return age;
    	}
    	
    }
    

3. 无参构造器

  • 作用

    • 创建对象,并对对象的的成员变量赋初值
  • 语法

    public 类名(){
    	方法体;
  • 注意事项

    • 如果自己不给出,系统会自动给出
  • 代码

    public class Person {
    	String name;
    	int age;
    	
    	
    	public Person() {
    		System.out.println("我是无参构造器");
    		this.name = "海风";
    		this.age = 24;
    	}
    }
    

4. 有参构造器

  • 作用

    • 解决无参构造器初始化成员初值时无法从外界输入的问题
  • 注意事项

    • 若果手动定义了有参构造器,而没有定义有参构造器,系统不会自动生成无参构造器
  • 总结

    • 多个有参构造器叫有参构造器的重载
  • 代码

    public class Person {
    	String name;
    	int age;
    	private String sex;
    	
    	
    	public Person() {
    		System.out.println("我是无参构造器");
    		this.name = "海风";
    		this.age = 24;
    		this.sex = "man";
    	}
    	
    	public Person(String name) {
    		this.name = name;
    	}
    }
    
    public class Demo02 {
    	public static void main(String[] args) {
    		Person person = new Person();
    		Person person1 = new Person("傻狗");
    		System.out.println(person.age);
    		System.out.println(person.name);
    		System.out.println(person1.name);
    	}
    }      
    

5. 成员变量赋值的两种方式的区别

  • 使用set方法

  • 使用有参构造器

  • 代码

    public class Computer {
    	String brand;
    	double price;
    	String color;
    	
    	public String getBrand() {
    		return brand;
    	}
    	
    	public void setBrand(String brand) {
    		this.brand = brand;
    	}
    	
    	public double getPrice() {
    		return price;
    	}
    	
    	public void setPrice(double price) {
    		this.price = price;
    	}
    	
    	public String getColor() {
    		return color;
    	}
    	
    	public void setColor(String color) {
    		this.color = color;
    	}
    	public Computer(String brand, double price, String color) {
    		super();
    		this.brand = brand;
    		this.price = price;
    		this.color = color;
    	}
    	public Computer() {
    		super();
    	}
    }
    
    public class Demo03 {
    	public static void main(String[] args) {
    		Computer computer = new Computer();
    		
    		//set方法赋值
    		computer.setBrand("华为");
    		computer.setColor("银色");
    		computer.setPrice(8999);
    		System.out.println(computer.price + "可以买到" + computer.brand + "的" + computer.color + "电脑");
    		
    		//有参构造赋值
    		Computer computer1 = new Computer("小米", 5999, "黑色");
    		System.out.println(computer1.price + "可以买到" + computer1.brand + "的" + computer1.color + "电脑");
    		
    	}
    }
    

6. 创建对象的步骤

tudent s = new Student();

  • 1,将main方法所在class类加载进内存方法区

  • 2,main方法进栈

  • 3,Student.class加载进内存方法区

  • 4,声明一个Student类型引用stu

  • 5,构造方法进栈,对对象中的属性赋值,构造方法弹栈

  • 6,将对象的地址值赋值给stu

  • 图解

在这里插入图片描述

7. 练习

  • 长方形的面积和周长

    public class Rectangle {
    	double length;
    	double width;
    	/**
    	 * @return the length
    	 */
    	public double getLength() {
    		return length;
    	}
    	public Rectangle() {
    		super();
    	}
    	public Rectangle(double length, double width) {
    		super();
    		this.length = length;
    		this.width = width;
    	}
    	/**
    	 * @param length the length to set
    	 */
    	public void setLength(double length) {
    		this.length = length;
    	}
    	/**
    	 * @return the width
    	 */
    	public double getWidth() {
    		return width;
    	}
    	/**
    	 * @param width the width to set
    	 */
    	public void setWidth(double width) {
    		this.width = width;
    	}
    	
    	public void getPerimeter () {
    		System.out.println("周长为:" + 2 * (length + width));
    	}
    	public void getArea () {
    		System.out.println("面积为:" + length * width);
    	}
    }
    
    
    public class Demo04_Rectangle_Text {
    	public static void main(String[] args) {
    		Rectangle rectangle = new Rectangle();
    		rectangle.length = 20;
    		rectangle.width = 30;
    		
    		rectangle.getPerimeter();
    		rectangle.getArea();
    		
    		Rectangle rectangle2 = new Rectangle(40, 60);
    		rectangle2.getPerimeter();
    		rectangle2.getArea();
    	}
    }
    

8.迪米特法则

  • 又称最小知道原则
    • 一个对象对其他对象有尽可能少的了解
  • 不要和陌生人说话

9. static关键字

  • 特点

    • 随类的加载而加载,且只加载一次
    • 优先于对象存在
    • 被类的所有对象共享
    • 节省内存
    • 可以直接使用类名调用
  • 总结

  • 需要共享成员是可以使用static

  • 代码

    
    public class Student {
    	String name;
    	static String gard;
    	
    	
    }
    
    
    public class Demo05_Static {
    	public static void main(String[] args) {
    		Student stu = new Student();
    		stu.name = "海风";
    		stu.gard = "2004";
    		
    		Student stu1 = new Student();
    		stu1.name = "求问号";
    		
    		Student stu2 = new Student();
    		stu2.name = "小王八";
    		
    		System.out.println(stu.name + "是" + stu.gard + "班的");
    		System.out.println(stu1.name + "是" + stu1.gard + "班的");
    		System.out.println(stu2.name + "是" + stu2.gard + "班的");
    }
    }
    
    • 图解

在这里插入图片描述

10. static关键字注意事项

  • 静态只能访问静态
  • 总结
    • 普通方法
      • 可以调用普通,也可以调用静态
    • 静态方法
      • 只能调用静态

11. 静态变量,成员变量,局部变量

  • 定义位置

    • 静态变量
      • 类中方法外
    • 成员变量
      • 类中方法外
    • 局部变量
      • 方法中
  • 所属不同

    • 静态变量
      • 属于类,又叫类变量
    • 成员变量
      • 属于对象,又叫对象变量
    • 局部变量
      • 属于方法,又叫方法变量
  • 内存位置

    • 静态变量
      • 方法区的静态方法区
    • 成员变量
      • 堆中
    • 局部变量
      • 栈中
  • 生命周期

    • 静态变量
      • 随着类的加载而加载,随着类的消失而消失
    • 成员变量
      • 随着对象的创建而存在,随着对象的消失而消失
    • 局部变量
      • 随着方法的执行而创建,随着方法的结束而销毁
  • 初始化

    • 静态变量
      • 系统给定初值
    • 成员变量
      • 系统给定初值
    • 局部变量
      • 系统不会给定初值

12. 自定义工具类

成员变量
* 类中方法外

  • 局部变量

    • 方法中
  • 所属不同

    • 静态变量
      • 属于类,又叫类变量
    • 成员变量
      • 属于对象,又叫对象变量
    • 局部变量
      • 属于方法,又叫方法变量
  • 内存位置

    • 静态变量
      • 方法区的静态方法区
    • 成员变量
      • 堆中
    • 局部变量
      • 栈中
  • 生命周期

    • 静态变量
      • 随着类的加载而加载,随着类的消失而消失
    • 成员变量
      • 随着对象的创建而存在,随着对象的消失而消失
    • 局部变量
      • 随着方法的执行而创建,随着方法的结束而销毁
  • 初始化

    • 静态变量
      • 系统给定初值
    • 成员变量
      • 系统给定初值
    • 局部变量
      • 系统不会给定初值

12. 自定义工具类

  • 方法绝大部分是静态的,方便调用

思维导图

思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ijava'pdax

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值