Java基础-面向对象编程(类、面向过程、面向对象、对象、属性、行为)(大象进冰箱)
- 1.面向对象与面向过程(大象门事件!!!)
- 2.类的组成
- 关键字(this、package、import)
- 4.大象进冰箱(代码实现)
- 《上一篇:运算、位运算、比较、循环与控制》
- 《 UML类图》
- 《目录:Java渐进式学习》
- 《幕》
1.面向对象与面向过程(大象门事件!!!)
1.1.面向过程(POP)
1.2.面向对象(OOP)
1.3.注意
2.类的组成
类、对象、构造器
/**
* 构造器
*
* 根据参数不同,构造器可以分为如下两类:
* 隐式无参构造器(系统默认提供)
* 显式定义一个或多个构造器(无参、有参)
*/
class GouZaoQi {
private int a = 1;
private String b = "hello";
public GouZaoQi() {
}
public GouZaoQi(int a) {
this.a = a;
}
public GouZaoQi(String b) {
this.b = b;
}
public GouZaoQi(int a, String b) {
this.a = a;
this.b = b;
}
public void hello() {
System.out.println(a +" "+ b);
}
}
public class TestGouZaoQi {
public static void main(String[] args) {
GouZaoQi gz1 = new GouZaoQi();
gz1.hello();// 1 hello
GouZaoQi gz2 = new GouZaoQi(999);
gz2.hello();// 999 hello
GouZaoQi gz3 = new GouZaoQi("ooo");
gz3.hello();// 1 ooo
GouZaoQi gz4 = new GouZaoQi(999, "ooo");
gz4.hello();// 999 ooo
}
}
匿名对象
方法
/**
* 构造器
*/
class GouZaoQi {
private int a = 1;
private String b = "hello";
//无参构造器
public GouZaoQi() {
}
//有参构造器
public GouZaoQi(int a) {
this.a = a;
}
//有参重载构造器
public GouZaoQi(String b) {
this.b = b;
}
//有参重载构造器
public GouZaoQi(int a, String b) {
this.a = a;
this.b = b;
}
//普通方法
public void hello() {
System.out.println(a + b);
}
//静态方法
public static void helloStatic(String str) {
System.out.println("静态方法: " + str);
}
}
public class TestGouZaoQi {
public static void main(String[] args) {
//通过对象调用普通方法
GouZaoQi gz1 = new GouZaoQi();
gz1.hello();//
//调用静态方法,通过类名调用
GouZaoQi.helloStatic("略略略~");
}
}
属性
/**
* 构造器
*/
class GouZaoQi {
//属性 a
public int a = 1;
//静态属性 b
public static String b = "hello";
//无参构造器
public GouZaoQi() {
}
}
public class TestGouZaoQi {
public static void main(String[] args) {
//通过对象调用,普通属性
GouZaoQi gz1 = new GouZaoQi();
System.out.println(gz1.a);
//调用静态属性,通过类名调用
System.out.println(GouZaoQi.b);
}
}
访问与权限
关键字(this、package、import)
this
/**
* 大象
*/
class DaXiang {
//大象名字
public String name;
//大象构造函数--
public DaXiang() {
this("默认");
}
//大象构造函数
public DaXiang(String name) {
this.name = name;
}
}
package
import
4.大象进冰箱(代码实现)
/**
* 大象
*/
class DaXiang {
//大象名字
public String name;
//大象构造函数
public DaXiang(String name) {
this.name = name;
}
//输出当前信息
@Override
public String toString() {
return "DaXiang{" +
"name='" + name + '\'' +
'}';
}
}
/**
* 冰箱
*/
class BingXiang {
//冰箱名字
public String name;
//冰箱构造函数
public BingXiang(String name) {
this.name = name;
}
public void 开门() {
System.out.println(name + ":开门");
}
public void 关门() {
System.out.println(name + ":关门");
}
public void 装东西(Object obj) {
System.out.println(name + ":装进一个 :" + obj.toString());
}
}
/**
* 人
*/
class Ren {
//人名
public String name;
//人构造函数
public Ren(String name) {
this.name = name;
}
//执行操作
public void start(DaXiang daXiang, BingXiang bingXiang) {
System.out.println(name + " : 开始了自己的操作");
bingXiang.开门();
bingXiang.装东西(daXiang);
bingXiang.关门();
}
}
/**
* 大象进冰箱
*/
public class DaXiangJinBingXiang {
public static void main(String[] args) {
DaXiang 大象 = new DaXiang("大象");
BingXiang 孩儿牌子冰箱 = new BingXiang("孩儿牌冰箱");
//
Ren ren = new Ren("小明");
ren.start(大象, 孩儿牌子冰箱);
}
}