封装和继承
类
什么是类
类:是一类具有相同属性和行为的事物的组合,本质还是一种数据变量.
属性:事物的静态的特征 (成员变量也叫做全局变量)
行为:事物的动态的特征 (方法;动作如:吃饭,睡觉)
类:对象的抽象 对其他类在进行抽象就是抽象类
抽象: 提取事物的共性,忽略事物的特性
如何创建类
例:
//属性 全局变量
String name;// 名字
int age;//年龄
int height;// 身高
int weight;//体重
// 行为 方法
public void eat(){
System.out.println("吃饭");
}// 吃饭
public void sleep(){
System.out.println("睡觉");
}// 睡觉
对象
什么是对象
对象:对象是真实存在的实体,对象是具体的,是类的具体实现(对象就是类的举例)
对象的特征
对象的创建
类名 对象名 = new 类名();
例:
Person person = new Person();
Person就是类名 也是数据类型(这里是引用)
person就是一个对象,也是一个变量
new Person()是在创建对象
类和对象之间的关系
类:物体属性和行为的模板
对象:物体的具体实现
封装
什么是封装
封装:是面向对象的三大特征之一
隐藏对象的属性和实现细节: 将我们的成员变量私有化,修饰符为private
仅对外提供公共访问和修改方式: 给我们的成员变量提供公共访问方式,getXXX() setXXX()
set属性(): 用来设置属性值的方法
get属性(): 用来获取属性值的方法
既然封装是面向对象的三大特征之一,那么我们在使用的时候,就应该遵循一些规则
封装的作用
封装最主要的功能在于我们能修改自己的实现代码,而不用修改那些调用我们代码的程序片段。
适当的封装可以让程序代码更容易理解与维护,也加强了程序式代码的安全性。
封装的优点
一、减少耦合
二、可重用性
三、精确控制
四、安全性
实现封装的步骤
封装使用的步骤:
1 成员变量私有化 private
2 提供公共访问方式 getXXX() setXXX()
封装实例
public class Animal01 {
/*
编写一个动物类
声明动物类的属性
声明动物类的方法
实例化一个动物,并给该动物添加属性,输出该动物的相关信息
*/
// 种类
private String variety;
//体重
private int weight;
// 行为
public void eat(){
System.out.println("正在觅食");
}
//构造方法
public Animal01() {
}
public Animal01(String variety, int weight) {
this.variety = variety;
this.weight = weight;
}
//供公共访问方式 getXXX() setXXX()
public String getVariety() {
return variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
public class People01 {
/*
编写-个人物类
声明人物类的属性
声明人物类的方法
实例化一个人物,并给该人物添加属性,输出该人物的相关信息
*/
//属性
// 名字
private String name;
//年龄
private int age;
// 身高
private int height;
//体重
private int weight;
// 行为
// 吃饭
public void eat(){
System.out.println("吃饭");
}
// 睡觉
public void sleep(){
System.out.println("睡觉");
}
//构造方法
public People01() {
}
public People01(String name, int age,int height,int weight) {
this.name = name;
this.height = height;
this.age = age;
this.weight = weight;
}
//提供公共访问方式 getXXX() setXXX()
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 int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
public class Instantiation01 {
public static void main(String[] args) {
//动物
Animal01 animal = new Animal01();
//set 修改 get 取得
animal.setVariety("大象");
animal.setWeight(1);
System.out.println("快看那是" + animal.getVariety() +
"它的体重有" + animal.getWeight() + "吨重" );
animal.eat();
//人
People01 people01 = new People01();
people01.setName("xxx");
people01.setAge(18);
people01.setHeight(163);
people01.setWeight(128);
System.out.println("她叫" + people01.getName() +
"年龄" + people01.getAge() +
"身高" + people01.getHeight()
+ "体重" + people01.getWeight());
people01.eat();
people01.sleep();
}
}
修饰符的分类
访问控制修饰符与非访问修饰符
访问修饰符:
1 private: 私有的,只能在本类中访问
2 default: 默认的,只能在本包中访问 (同一个文件夹中)
3 protected: 受保护的,只能在本包中访问,子类可以访问
4 public: 公共的,都可以访问
包: 本质就是一个文件夹,用来管理我们的类的,我们的类都要放在包中。我们某些相同功能的类,可以放在同一个包中
包名: 一般都是公司的域名倒着写
非访问修饰符:
static: 静态的,修饰成员变量和成员方法的。static修饰的成员变量和成员方法,不能直接调用非静态的成员变量和成员方法
final: 最终的,修饰类,修饰成员变量,修饰成员方法
abstract :用来创建抽象类和抽象方法
**synchronized **:主要用于线程的编程,synchronized 关键字声明的方法同一时间只能被一个线程访问。synchronized 修饰符可以应用于四个访问修饰符
**volatile **:volatile 修饰的成员变量在每次被线程访问时,都强制从共享内存中重新读取该成员变量的值。而且,当成员变量发生变化时,会强制线程将变化值回写到共享内存。这样在任何时刻,两个不同的线程总是看到某个成员变量的同一个值。
修饰类: 不能被继承
修饰成员变量: 不能被修改,赋值一次
修饰成员方法: 不能被重写
继承
什么是继承
继承:java面向对象三大特征之一
含义: 子类继承父类,可以直接使用父类的属性和方法(主要使用方法)
父类: 有时候也会被别人叫做 超类、基类。
关键字: extends
继承: 类和类之间的关系
一般什么时候用继承: 一般一个小的类和大的类之间这样使用。
好处是: 减少代码的冗余
继承的特性
1、子类可以继承父类非private属性和方法
2、子类可以有自己特有的属性和方法
3、java类的继承是单继承,但是可以多重继承
4、子类可以重写父类的方法。
继承的关键字
extends:在 Java 中,类的继承是单一继承,也就是说,一个子类只能拥有一个父类,所以 extends 只能继承一个类
继承实例
一、使用继承实现卖车系统
创建轿车类(品牌、型号、售价、百公里油耗、发动机类型),卡车类(品牌、型号、售价、最大载重、可拉货物类型)。
用户选择要买购买的汽车种类并付钱,付钱后显示买到的汽车参数。
/**
* 总类
*/
import java.util.Scanner;
public class MotorVehicle03 {
private String brand; //品牌
private String type; // 型号
private int price; //售价
public MotorVehicle03() {
}
public MotorVehicle03(String brand, String type, int price) {
this.brand = brand;
this.type = type;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "MotorVehicle03{" +
"brand='" + brand + '\'' +
", type='" + type + '\'' +
", price=" + price +
'}';
}
public void pay(){
System.out.println("您选择购买了" + this.brand + "请付款" + this.price);
Scanner scanner = new Scanner(System.in);
int money = scanner.nextInt();
while (money < this.price){
System.out.println("您给的钱不够");
money = scanner.nextInt();
}
System.out.println("找零" + (money-this.price));
System.out.println("恭喜你购买了一台" + this.brand);
}
public void show(){
System.out.println("您购买的" + this.brand + "型号为:");
System.out.println("品牌:" + this.brand);
System.out.println("型号:" + this.type);
System.out.println("售价:" + this.price);
}
}
/**
* 轿车类
*/
public class Car03 extends MotorVehicle03{
private String consumption; //百公里油耗
private String engineType; //发动机类型
public Car03(String consumption, String engineType) {
this.consumption = consumption;
this.engineType = engineType;
}
public Car03(String brand, String type, int price, String consumption, String engineType) {
super(brand, type, price);
this.consumption = consumption;
this.engineType = engineType;
}
public Car03() {
}
public String getConsumption() {
return consumption;
}
public void setConsumption(String consumption) {
this.consumption = consumption;
}
public String getEngineType() {
return engineType;
}
public void setEngineType(String engineType) {
this.engineType = engineType;
}
@Override
public String toString() {
return "Car03{" +
"consumption='" + consumption + '\'' +
", engineType='" + engineType + '\'' +
'}';
}
public void showCar(){
super.show();
System.out.println("百公里油耗" + this.consumption);
System.out.println("发动机类型" + this.engineType);
}
}
/**
* 卡车类
*/
public class Truck03 extends MotorVehicle03{
private String capacity;//最大载重
private String typeOfCargoAvailable; //可拉货物类型
public Truck03() {
}
public Truck03(String capacity, String typeOfCargoAvailable) {
this.capacity = capacity;
this.typeOfCargoAvailable = typeOfCargoAvailable;
}
public Truck03(String brand, String type, int price, String capacity, String typeOfCargoAvailable) {
super(brand, type, price);
this.capacity = capacity;
this.typeOfCargoAvailable = typeOfCargoAvailable;
}
public String getCapacity() {
return capacity;
}
public void setCapacity(String capacity) {
this.capacity = capacity;
}
@Override
public String toString() {
return "Truck03{" +
"capacity='" + capacity + '\'' +
", typeOfCargoAvailable='" + typeOfCargoAvailable + '\'' +
'}';
}
public void showTruck(){
super.show();
System.out.println("最大载重" + this.capacity);
System.out.println("拉货类型" + this.typeOfCargoAvailable);
}
}
/**
* 运行类
*/
import java.util.Scanner;
import java.util.Timer;
import java.util.TreeSet;
public class CarSellingSystem03 {
public static void main(String[] args) throws InterruptedException {
System.out.println("-----------欢迎来到小赵汽车---------");
System.out.println("1.小坐一会 2.随便看看 3.试驾一下");
Scanner scanner = new Scanner(System.in);
int choose = scanner.nextInt();
while (choose > 3 || choose < 1){
System.out.println("输入错误");
choose = scanner.nextInt();
}
if (choose == 1){
Thread.sleep(5000);
System.out.println("你已经做了五个小时,该回家了");
}else if (choose == 2){
System.out.println("1.轿车 2.卡车");
int choose1 = scanner.nextInt();
while (choose1 > 2 || choose1 < 1){
System.out.println("输入错误");
choose1 = scanner.nextInt();
}
if (choose1 == 1){
Car03 car03 = new Car03("五菱","mini",500000,"7L","八缸");
car03.pay();
car03.showCar();
}else {
Truck03 truck03 = new Truck03("解放", "JH6", 1000000, "10吨", "牲畜");
truck03.pay();
truck03.showTruck();
}
}else {
System.out.println("试驾一会");
}
}
}
二、使用继承与封装实现零食商店系统
创建食物(饥饱值、食物净重、食物味道、品种)、油炸(品种)、非油炸(品种)。
根据用户选择,初始化用户的零食对象,并输出零食信息。
public class Food03 {
private String satietyValue; //饥饱值
private String netFoodWeight;//食物净重
private String foodTaste;//食物味道
private String variety;// 品种
public Food03(String satietyValue, String netFoodWeight, String foodTaste, String variety) {
this.satietyValue = satietyValue;
this.netFoodWeight = netFoodWeight;
this.foodTaste = foodTaste;
this.variety = variety;
}
public Food03() {
}
public String getSatietyValue() {
return satietyValue;
}
public void setSatietyValue(String satietyValue) {
this.satietyValue = satietyValue;
}
public String getNetFoodWeight() {
return netFoodWeight;
}
public void setNetFoodWeight(String netFoodWeight) {
this.netFoodWeight = netFoodWeight;
}
public String getFoodTaste() {
return foodTaste;
}
public void setFoodTaste(String foodTaste) {
this.foodTaste = foodTaste;
}
public String getVariety() {
return variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
@Override
public String toString() {
return "Food02{" +
"satietyValue='" + satietyValue + '\'' +
", netFoodWeight='" + netFoodWeight + '\'' +
", foodTaste='" + foodTaste + '\'' +
", variety='" + variety + '\'' +
'}';
}
public void show(){
System.out.println("饥饱值" + this.satietyValue);
System.out.println("食物净重" + this.netFoodWeight);
System.out.println("食物味道" + this.foodTaste);
System.out.println("品种" + this.variety);
}
}
/**
* 油炸
*/
public class DeepFry03 extends Food03{
private String option01;
private String option02;
private String option03;
public DeepFry03(String satietyValue, String netFoodWeight, String foodTaste, String variety) {
super(satietyValue, netFoodWeight, foodTaste, variety);
}
public DeepFry03(String option01, String option02, String option03) {
this.option01 = option01;
this.option02 = option02;
this.option03 = option03;
}
public DeepFry03() {
}
public String getOption01() {
return option01;
}
public void setOption01(String option01) {
this.option01 = option01;
}
public String getOption02() {
return option02;
}
public void setOption02(String option02) {
this.option02 = option02;
}
public String getOption03() {
return option03;
}
public void setOption03(String option03) {
this.option03 = option03;
}
@Override
public String toString() {
return option01 + option02 + option03 ;
}
}
/**
* 非油炸
*/
public class NonFried03 extends Food03{
private String option01;
private String option02;
private String option03;
public NonFried03(String satietyValue, String netFoodWeight, String foodTaste, String variety) {
super(satietyValue, netFoodWeight, foodTaste, variety);
}
public NonFried03(String satietyValue, String netFoodWeight, String foodTaste, String variety, String option01, String option02, String option03) {
super(satietyValue, netFoodWeight, foodTaste, variety);
this.option01 = option01;
this.option02 = option02;
this.option03 = option03;
}
public NonFried03(String option01, String option02, String option03) {
this.option01 = option01;
this.option02 = option02;
this.option03 = option03;
}
public NonFried03() {
}
public String getOption01() {
return option01;
}
public void setOption01(String option01) {
this.option01 = option01;
}
public String getOption02() {
return option02;
}
public void setOption02(String option02) {
this.option02 = option02;
}
public String getOption03() {
return option03;
}
public void setOption03(String option03) {
this.option03 = option03;
}
@Override
public String toString() {
return option01 + option02 + option03 ;
}
}
/**
* 运行类
*/
import java.util.Scanner;
public class SnackShop03 {
public static void main(String[] args) {
System.out.println("---------欢迎来到小赵零食店1---------");
System.out.println("1.油炸食品 2.非油炸食品");
Scanner scanner = new Scanner(System.in);
int choose = scanner.nextInt();
while (choose > 2 || choose < 1){
System.out.println("请重新输入");
choose = scanner.nextInt();
}
if (choose == 1){
DeepFry03 deepFry03 = new DeepFry03("1.原味干脆面","2.黄瓜味薯片","3.辣条");
System.out.println(deepFry03);
int choose1 = scanner.nextInt();
while (choose1 > 3 || choose1 < 1){
System.out.println("请重新输入");
choose1 = scanner.nextInt();
}
if (choose1 == 1){
System.out.println("成功购买了" + deepFry03.getOption01());
DeepFry03 deepFry = new DeepFry03("1","150克","原味","油炸食品");
deepFry.show();
}else if (choose1 == 2){
System.out.println("成功购买了" + deepFry03.getOption02());
DeepFry03 deepFry = new DeepFry03("2","200克","黄瓜味","油炸食品");
deepFry.show();
}else {
System.out.println("成功购买了" + deepFry03.getOption03());
DeepFry03 deepFry = new DeepFry03("0.1","150克","辣味","油炸食品");
deepFry.show();
}
}else {
NonFried03 nonFried03 = new NonFried03("1.山楂锅魁","2.肉松蛋糕","3.枣花酥");
System.out.println(nonFried03);
int choose1 = scanner.nextInt();
while (choose1 > 3 || choose1 < 1){
System.out.println("请重新输入");
choose1 = scanner.nextInt();
}
if (choose1 == 1){
System.out.println("成功购买了" + nonFried03.getOption01());
NonFried03 nonFried = new NonFried03("3","500克","山楂味","非油炸食品");
nonFried.show();
}else if (choose1 == 2){
System.out.println("成功购买了" + nonFried03.getOption02());
NonFried03 nonFried = new NonFried03("5","1500克","肉松味","非油炸食品");
nonFried.show();
}else {
System.out.println("成功购买了" + nonFried03.getOption03());
NonFried03 nonFried = new NonFried03("7","500克","红枣味","非油炸食品");
nonFried.show();
}
}
}
}
Java继承关系中各个代码块的运行顺序
同一个类里面: 静态>代码块>构造方法
子父类里面: 父类静态>子类静态>父类代码块>父类构造方法>子类代码块>子类构造方法
类创建要比对象早,static是属于类的,不属于对象。
在静态的里面不要调用非静态的,也不要用this和super
包
什么是包
为了更好地组织类,Java 提供了包机制,用于区别类名的命名空间。
包的使用
1 为了方便管理类
2 域名倒写
3 全部小写
4 import和package
如何导入包
import 导入包 告诉下面的代码,我要用那个包里的那个类
package 告诉我们当前的类属于那个包
完整的类名: 全限定类名