面向对象3.0
多类合作
引擎类
轮胎类
汽车类
把引擎类对象和键盘类对象作为成员变量给汽车类
package com.a.work;
/**
* 引擎类
*
* @author homework
*
*/
public class Engine {
private String name;
private String type;
public Engine() {}
public Engine(String name, String type) {
super();
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;
}
}
package com.a.work;
/**
* 轮胎类
*
* @author homework
*
*/
public class Wheel {
private String type;
private int size;
public Wheel() {}
public Wheel(String type, int size) {
this.type = type;
this.size = size;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
package com.a.work;
/**
* 汽车类
*
* @author homework
*
*/
public class Car {
private Engine engine;
private Wheel wheel;
public Car() {
super();
}
public Car(Engine engine, Wheel wheel) {
super();
this.engine = engine;
this.wheel = wheel;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public Wheel getWheel() {
return wheel;
}
public void setWheel(Wheel wheel) {
this.wheel = wheel;
}
public void show( ) {
System.out.println("引擎名字:" + engine.getName() + "引擎型号:" + engine.getType());
System.out.println("轮胎型号" + wheel.getType() + "轮胎尺寸" + wheel.getSize());
}
}
package com.a.work;
public class Demo2 {
public static void main(String[] args) {
Engine engine = new Engine("米其林" , "xiaomi178");
Wheel wheel = new Wheel("公牛",998);
Car car = new Car(engine,wheel);
car.show();
System.out.println("换轮胎");
Wheel wheel2 = new Wheel("二号轮胎",99);
car.setWheel(wheel2);
car.show();
}
}
引擎名字:米其林引擎型号:xiaomi178
轮胎型号公牛轮胎尺寸998
换轮胎
引擎名字:米其林引擎型号:xiaomi178
轮胎型号二号轮胎轮胎尺寸99
总结
-
类和对象的操作模式
类是数据类型定义
对象是目前操作成员方法,操作成员变量的核心 -
这两组代码在main方法中基本上全部是一个面向对象思想
a. 自定义数据类型,自定义类对象,作为方法参数。
b. 通过类对象来操作代码方式,所有的内容都是和对象相关 -
代码需要阅读,一定要阅读!!!
不要断章取义!!!
. 就是的 -
代码中要什么你给什么
匿名对象
Person person = new Person(“骚杰”, 66, ‘男’);
Person 类名
person 对象名
new Person(…) 像内存的堆区申请空间,创建一个Person类对象使用的内存空间
匿名对象
没有名字的对象,没有对象名的对象
格式:
new 构造方法(所需参数)
用途
1. 提高开发效率,隐形眼镜日抛,一次性筷子
匿名对象当前行使用之后,如果没有其他引用数据类型的变量保存其地址,直接销毁
2. 简化代码结构
3. 通过匿名对象直接调用成员方法
4. 使用匿名对象作为方法的参数
继承
继承在开发中是一种抽象的归纳总结思想,剥离通用的属性,通用的方法,简化之后的开发压力。只需要在继承之后,满足个性化的实现即可。
通过继承可以优化代码,在JavaWEB开发中,继承大多数情况下,都是用来提高方法的复用度
关键字:
extends
格式:
class A extends B {
}
A类是B类的一个子类
B类是A类的唯一父类
【Java是一门单继承语言,一个类有且只能继承一个父类】
package com.qfedu.c_extends;
class Father {
public int height;
private int testPrivate;
public void game() {
System.out.println("钓鱼,象棋~~~");
}
private void privateMethod() {
System.out.println("私有化方法");
}
}
/*
* Son类是Father类的一个子类
* Father类是Son类的唯一父类
*/
class Son extends Father {
public int age;
public void study() {
System.out.println("子类学习方法!!!好好学习,天天向上!");
}
}
public class Demo1 {
public static void main(String[] args) {
// 创建一个Father类对象
Father father = new Father();
// 通过Father类对象,使用father类内的成员变量和成员方法
father.height = 170;
father.game();
System.out.println(father.height);
System.out.println("------------------------------------");
// 创建一个Son类对象
Son son = new Son();
// 使用Son类对象,调用Son类自己的成员变量和成员方法
son.age = 16;
son.study();
System.out.println(son.age);
// 使用Son类的对象,调用通过继承得到的父类【内容】
// 可以使用Father类内height 成员变量,height成员变量是使用public修饰的
son.height = 172;
// 可以使用Father类内的game 成员方法,game成员方法使用public修饰
son.game();
// private修饰的成员变量,和成员方法不能继承给子类使用
// The field Father.testPrivate is not visible
// son.testPrivate = 10;
// The method privateMethod() from the type Father is not visible
// son.privateMethod();
}
}
- A类使用extends关键字继承B类
A类是B类的一个子类,B类是A类的唯一父类 - 继承之后,子类可以通过继承得到父类中非私有化成员变量,和非私有化成员方法
- 继承之后,子类不可以通过继承得到父类中的私有化成员变量,和私有化成员方法。
private修饰的内容有且只能在类内使用!!!