类的封装
通过setter和getter实现类的私有化和对外提供访问和设置
示例1
package edu.xcdq;
/*
类的封装 1 把属性私有化 2 用方法对外提供访问或设置
*/
public class Person {
private String name;
private int age;
private double high;
// getter setter
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setAge( int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public void setHigh(double high) {
this.high = high;
}
public double getHigh() {
return this.high;
}
}
示例2
package edu.xcdq;
public class Product {
private int id;
private double price;
private int count;
// setter getter
public void setId(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
public void setPrice( double price) {
this.price = price;
}
public double getPrice() {
return this.price;
}
public void setCount() {
this.count = count;
}
public int getCount() {
return this.count;
}
}
方法的封装
package edu.xcdq;
public class Demo01 {
public static void main(String[] args) {
System.out.println( 80 * 0.4 + 75 * 0.6);
System.out.println( 80 * 0.4 + 75 * 0.6);
System.out.println( 80 * 0.4 + 75 * 0.6);
System.out.println( 80 * 0.4 + 75 * 0.6);
System.out.println( 80 * 0.4 + 75 * 0.6);
System.out.println( 80 * 0.4 + 75 * 0.6);
System.out.println(calc(80,75));
System.out.println(calc(80,75));
System.out.println(calc(80,75));
System.out.println(calc(80,75));
System.out.println(calc(80,75));
System.out.println(calc(80,75));
}
// 方法的封装 把复杂的操作放在一个函数里,用户只需要关心输入和输出
public static double calc( int pingshi , int qimo ) {
return pingshi * 0.4 + qimo * 0.6 ;
}
}