1
package com.company;
/*
* 类的封装 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 age;
}
public double getHigh() {
return high;
}
public void setHigh(double high) {
this.high = high;
}
}
2
package com.company;
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 id;
}
public void setPrice(double price) {
this.price = price;
}
public double getPrice () {
return price;
}
public void setCount (int count) {
this.count = count;
}
public int getCount () {
return this.count;
}
}
3
public class Domo01 {
public static void main(String[] args) {
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;
}
}