分包
基本作用:防止了类的重名问题
项目作用:项目中有众多的类,把相同功能的类放在同一个包中,方便管理
工具类:com.dream.util/tool
实体类:com.dream.vo/bean/entity/bto/pojo
数据库类:com.dream.dao/mapper
1. 把大象装进冰箱(面向对象)
需求:小明把大象装进冰箱 分析: 大象类: 属性:姓名,种类 方法:吃饭,叫 冰箱类: 属性:品牌,价格,容量 方法:开门,关门,装大象 人类: 属性:姓名,性别,年龄 方法:开冰箱们,关冰箱门,装大象 编写类的流程 1.编写属性 2.无参构造 3.有参构造 4.get set方法 5.功能编写
package com.qf.test01;
public class test01 {
/**
* 需求:小明把大象装进冰箱
*分析:
* 大象类:
* 属性:姓名,种类
* 方法:吃饭,叫
* 冰箱类:
* 属性:品牌,价格,容量
* 方法:开门,关门,装大象
* 人类:
* 属性:姓名,性别,年龄
* 方法:开冰箱们,关冰箱门,装大象
* 编写类的流程
* 1.编写属性
* 2.无参构造
* 3.有参构造
* 4.get set方法
* 5.功能编写
*/
public static void main(String[] args) {
Person p =new Person("小明",18,'男');
Refrigerator r=new Refrigerator("huawei",8888,2000);
Elephant e=new Elephant("小飞象","亚洲象");
p.open(r);
p.put(e,r);
p.close(r);
}
}
package com.qf.test01;
public class Elephant {
private String name;
private String type;
public Elephant(){
}
public Elephant(String name, String type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void eat(){
System.out.println(this.name+"吃东西");
}
public void shout(){
System.out.println(this.name+"叫:嗷嗷嗷~~~");
}
}
package com.qf.test01;
public class Refrigerator {
private String brand;
private double price;
private double capacity;
public Refrigerator(){
}
public Refrigerator(String brand, double price, double capacity) {
this.brand = brand;
this.price = price;
this.capacity = capacity;
}
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 double getCapacity() {
return capacity;
}
public void setCapacity(double capacity) {
this.capacity = capacity;
}
public void openDoor(){
System.out.println(this.brand+"冰箱门开");
}
public void closeDoor(){
System.out.println(this.brand+"冰箱门关");
}
public void loadElephant(Elephant e){
e.eat();
e.shout();
System.out.println(this.brand+"装"+e.getName());
}
}
package com.qf.test01;
public class Person {
String name;
int age;
char sex;
public Person(String name, int age, char sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
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;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public void open(Refrigerator r){
r.openDoor();
}
public void close(Refrigerator r){
r.closeDoor();
}
public void put(Elephant e,Refrigerator r){
r.loadElephant(e);
}
}
2. static修饰属性
package com.qf.test02;
public class test1 {
/**
* 知识点:static修饰属性
* 注意:静态属性属于类属性,直接使用类名调用(如果使用对象调用会有小黄线(警告))
* 需求:做实验,学习静态属性
* */
public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
a1.str1="aaa";
a2.str1="bbb";
System.out.println(a1.str1);//aaa
System.out.println(a2.str1);//bbb
// a1.str2="XXX";
// a2.str2="yyy";
// System.out.println(a1.str2);
// System.out.println(a2.str2);
A.str2="xxx";
A.str2="yyy";
System.out.println(A.str2);//yyy
}
}
package com.qf.test02;
public class A {
//成员属性
String str1;
//静态属性
static String str2;
}