1.面向对象的三大特征
封装、继承、多态
2.封装
2.1不局限于面向对象的封装
方法的封装
工具类的封装
框架的封装
需要拥有封装的思想。可以用于整合的知识点
2.2符合javaBean规范的类封装过程
代码规范非常重要 ,后期可以有效提高我们的开发效率。
在java中定义符合JavaBean规范的类有什么要求
1.所有成员变量必须全部私有化==>private
2.必须提供一个无参构造方法
3.要求使用private修饰的成员变量提供对应的操作方法==>Setter Getter
2.2.1private关键字
private关键字是一个权限修饰符 :
private修饰的成员变量,成员方法【构造方法】都是私有化内容,球且只能在类内使用,类外没有任何的操作权限。
package day06;
/**
*
* private 关键字使用
* @author
*
*/
class Dog{
private String name;
int age;
char gender;
public void testFiled(){
//类内可以直接使用私有化private修饰的成员变量
name="Bobo";
test();
}
private void test(){
System.out.println("你好");
}
}
public class Demo1 {
public static void main(String[] args) {
Dog dog = new Dog();
//没有使用private约束的情况下,类外可以使用
//当成员变量使用private修饰之后,当前成员变量类外没有操作权限
dog.name="王可可";
dog.age=5;
dog.gender='雌';
}
}
2.2.2Setter和Getter方法
private修饰的成员变量类外是没有任何操作权限,这里需要提供对应的操作方法,Setter和Getter方法
Setter方法格式:
pubic void set成员变量名(对应成员变量的数据类型 成员变量的形式参数){
this.成员变量名= 成员变量的形式参数;
}
public void setHello(String hello) {
this.hello = hello;
}
Getter方法格式:
pubic 对应成员变量的数据类型 get成员变量名(){
return 成员变量;
}
public String getHello() {
return hello;
}
如果成员变量是一个boolean类型,Getter方法有所不同
boolean married;
格式:
public boolean isMarried{
return 成员变量;
}
package day06;
/**
* 按照javaBean规范完成自定义类
* @author
*
*/
class Cat{
private String name;
private int age;
private char gender;
private boolean married;
//注意
//根据实际需要完成对应Constructor ,Setter,Getter
//shift+ alt +s
//无参构造方法
public Cat(){
super();
}
//有参构造方法
public Cat(String name,int age,char gender,boolean married){
super();
this.name=name;
this.gender=gender;
this.age=age;
this.married=married;
}
//setter和getter方法
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 getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public boolean isMarried() {
return married;
}
public void setMarried(boolean married) {
this.married = married;
}
}
public class Demo2 {
public static void main(String[] args) {
Cat cat =new Cat();
cat.setName("小二");
cat.setAge(2);
cat.setGender('雌');
cat.setMarried(false);
System.out.println("name"+cat.getName());
System.out.println("age"+cat.getAge());
System.out.println("Gender"+cat.getGender());
System.out.println("isMarried"+cat.isMarried());
}
}
3.多类合作
3.1什么是多类合作
在开发过程中,除了基本数据类型,大多数情况下,都是类对象操作数据,作为
1.方法的参数
2.类定义时成员变量数据类型
3.2方法的参数(电脑和维修店案例)
需求:
电脑类
属性: 屏幕是否正常 boolean ok;
方法:电脑屏幕如果是OK的,可以正常看视频,否则无法观看
维修店类:
属性:店址 电话 店名
方法:修电脑
Computer类
package Day07;
/**
* 电脑类
* @author
*
*/
public class Computer {
private boolean screenShow;
public Computer(){}
public Computer(boolean screenShow){
this.screenShow=screenShow;
}
public boolean isScreenShow() {
return screenShow;
}
public void setScreenShow(boolean screenShow) {
this.screenShow = screenShow;
}
/**
* 判断当前屏幕是否正常,正常可以观看 否则不能
*/
public void computerRunning(){
//判断屏幕状态
if(screenShow){
System.out.println("正常可以观看");
}else {
System.out.println("不能正常观看");
}
}
}
main类
package Day07;
/**
* main方法所在类
* @author
*
*/
public class Demo1 {
public static void main(String[] args) throws InterruptedException {
Computer computer =new Computer();
for(int i =0;i<10;i++){
}
computer.setScreenShow(false);
System.out.println("电脑404");
computer.computerRunning();
Thread.sleep(500);
Factory factory = new Factory();
factory.setName("小刘专修");
factory.setAddr("刘兰壮");
factory.setTele("110");
//通过修理店对象,调用修理电脑的方法
//需要的参数是Computer类对象
factory.repair(computer);
for(int i =0;i<10;i++){
computer.computerRunning();
Thread.sleep(500);
}
}
}
factory类
package Day07;
/*
* 修理店类
*/
public class Factory {
private String name;
private String addr;
private String tele;
public Factory() {
super();
}
public Factory(String name, String addr, String tele) {
super();
this.name = name;
this.addr = addr;
this.tele = tele;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getTele() {
return tele;
}
public void setTele(String tele) {
this.tele = tele;
}
/**
* 核心任务修电脑
*
* 方法分析:
* public 公开的
* 返回值类型
* void
* 方法名:
* repair
* 形式参数列表:
* @throws InterruptedException
*
*
*/
public void repair(Computer computer) throws InterruptedException{
if(false ==computer.isScreenShow()){
System.out.println("有问题,修理中");
Thread.sleep(1000);
computer.setScreenShow(true);
System.out.println("修理完毕");
}else{
System.out.println("m没毛病");
}
}
}
3.3成员变量的数据类型为自定义类型
汽车
class Car
Engine engine;
Type type;
发动机:发动机也需要一个类
class Engine
型号
排量
轮胎:轮胎也需要一个类
class Type
型号
尺寸
package day08;
/*
* 汽车类
* 成员变量
* Engine对象
* Type对象
*/
public class Car {
/*
* 这里需要一个Engine对象
*/
private Engine engine;
/*
* 这里需要 Type类对象
*/
private Type type;
public Car(){
/**
*
*/
}
public Car(Engine engine, Type type) {
super();
this.engine = engine;
this.type = type;
}
/**
* 获取当前Car类对象
* @return
*/
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public void show(){
System.out.println("Engine Name"+ engine.getName()+"Capacity"+engine.getCapacity());
System.out.println("Type Name"+ type.getName()+"Size"+type.getSize());
}
}
Engine类
package day08;
public class Engine {
private String name;
private float capacity;
public Engine() {
super();
}
public Engine(String name, float capacity) {
super();
this.name = name;
this.capacity = capacity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getCapacity() {
return capacity;
}
public void setCapacity(float capacity) {
this.capacity = capacity;
}
}
Type类
package day08;
/*
* 轮胎类
*/
public class Type {
private String name;
private int size;
public Type() {
super();
}
public Type(String name, int size) {
super();
this.name = name;
this.size = size;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
@Override
public String toString() {
return "Type [name=" + name + ", size=" + size + "]";
}
}
Main类
package day08;
/**
* 存放main方法
* @author 刘
*
*/
public class MainProject {
public static void main(String[] args) {
/*
* 需要组装汽车,这里引擎类对象和轮胎类对象
*/
Engine engine = new Engine("EA888",2.0F);
Type type = new Type("米其林",19);
Car car= new Car(engine,type);
car.show();
System.out.println("=====================");
Engine engine2 = new Engine("EA1331",1.6F);
car.setEngine(engine2);
car.show();
Type type2 = new Type("马牌",18);
System.out.println("===============");
car.setType(type2);
car.show();
}
}
4.匿名对象
匿名对象:
new 构造方法(必要的参数)
匿名对象的用途:
1.使用匿名对象直接调用类内的成员方法
class SingleDog{
String name;
public SingleDog(){
}
public SingleDog(String name) {
super();
this.name = name;
}
void test() {
// TODO Auto-generated method stub
System.out.println("SingleDog类成员变量方法");
}
}
public class anonymous {
public static void main(String[] args) {
SingleDog singleDog =new SingleDog();
singleDog.name="网二";
singleDog.test();
//匿名对象直接调用类内的成员变量
new SingleDog().test();
new SingleDog().name="介个";
System.out.println(new SingleDog().name);
}
}
2.匿名对象直接作为方法使用
import java.io.BufferedInputStream;
class SingleDog{
String name;
public SingleDog(){
}
public SingleDog(String name) {
super();
this.name = name;
}
void test() {
// TODO Auto-generated method stub
System.out.println("SingleDog类成员变量方法");
}
}
public class anonymous {
public static void main(String[] args) {
SingleDog singleDog =new SingleDog();
singleDog.name="网二";
singleDog.test();
//匿名对象直接调用类内的成员变量
new SingleDog().test();
new SingleDog().name="介个";
System.out.println(new SingleDog().name);
//这里可以传入一个SIngleDog对象
testObject(singleDog);
//传入一个匿名对象作为方法的参数
testObject(new SingleDog());
// new BufferedInputStream(new FilStr)
}
public static void testObject(SingleDog singledog){
System.out.println(singledog);
}
}
注意:使用匿名对象不要操作成员变量,有可能是有去无回
优势:
1.阅后即焚,匿名对象在使用之后,立即被JVM GC收回
2.解决内存空间,提高效率,简化代码书写
5.继承
5.1java中实现继承的方式
继承使用的关键字 extends
格式:classA extends B{
}
A类是B类的一个子类
B类是A类的唯一父类
java中的继承是一个单继承模式
package day09;
class Father{
//public 修饰的公开成员变量name
public String name;
//private修饰的私有化成员变量salary
private double salary;
public Father() {
super();
}
public Father(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
/*
* public 修饰的公开方法
*/
public void game(){
System.out.println("黄建矿工");
}
private void testPrivate(){
System.out.println("父类私有化方法");
}
}
/**
* Son类是Father类的一个子类
* Father类是Son类的唯一父类
* @author 刘朝阳
*
*/
class Son extends Father{
int age;
public void work(){
System.out.println("都播程序员");
}
}
public class Demo1 {
public static void main(String[] args) {
Father father =new Father();
father.name="建林";
father.game();
Son son =new Son();
//可以使用自定义的成员变量和成员方法
son.age=18;
son.work();
//通过继承之后,可以获取到父类的非私有化成员变量和成员方法
son.name="王校长";
son.game();
}
}
基本要求:
1.子类继承父类之后,可以使用父类的非私有化成员变量和成员方法【非私有化】
2.子类不能继承得到父类的私有化内容
5.3继承的问题
问题:Father类构造方法
Son类的构造方法
调用子类构造方法时,发现父类的无参数构造方法会自动调用咸鱼子类构造方法执行
有其父必有其子
5.3.2为什么会自动执行父类的无参数的构造方法
这里有一个super关键字
1.调用父类成员变量的
super关键字调用父类的构造方法
package day09;
class Animal{
//public修饰成员变量,并且赋予默认值
public String name = "动物";
public void eat(){
System.out.println("父类eat方法");
}
}
class Dog extends Animal{
public String name = "小狗";
public void eat() {
// TODO Auto-generated method stub
System.out.println("子类eay方法");
}
public void test() {
// TODO Auto-generated method stub
//就近元素,name对应的是Dog类内的成员变量
System.out.println(name);
//需要使用父类的name属性
System.out.println(super.name);
//就近原则,子列eat方法
eat();
//使用super明确告知这里使用的是父类的成员变量
super.eat();
}
}
public class Demo2 {
public static void main(String[] args) {
new Dog().test();
}
}
super(实际参数)
java编译器会根据实际参数的数据类型,参数顺序,选择对应的父类构造方法执行,初始化父类的成员空间,方法重载机制
package day09;
class LOLHero{
public int hp;
public int mp;
public LOLHero() {
System.out.println("无参数构造方法");
}
public LOLHero(int hp){
this.hp=hp;
System.out.println("单参数构造方法");
}
public LOLHero(int hp, int mp) {
super();
this.hp = hp;
this.mp = mp;
System.out.println("两个参数构造方法");
}
}
class VN extends LOLHero{
public VN(){
super();
System.out.println("VN构造方法");
}
}
public class Demo3 {
public static void main(String[] args) {
new VN();
}
}
特征:
1.如果没有显示调用父类的构造方法,默认java编译器会调用无参父类构造方法使用
2.根据数据类型选择对应方法
3.super调用构造方法,必须在当前构造方法的第一行。