首页 > 技术文章 > day03

zzzao 2019-06-18 22:49 原文

day03

一、面向对象
(1)三大特性
a.封装
权限修饰符
public 公开的
protectesd 保护的、一个体系,继承时只有继承父类的子类能用,其他都不能用
private 私有的、一个类内,子类继承时也无法访问父类属性
作用点:修饰类、方法、属性

package com.OnClass.day3.Demo;

public class OopDemo {
/**
* 私有,其他类都不能访问使用这个属性
*/
private String name;


/**
* 公开,不同的包下都可以访问这个属性
*/

public int age;

/**
* 受保护的,只有子类可以访问这个权限
*/

protected String hobby;
/**
* 默认的
*/
String id;

public void setId(String id) {
this.id = id;
}

public OopDemo(int age) {
this.age = age;
}

public static void main(String[] args) {
/**
* 1、子类对象无法访问父类私有属性name
* 2、子类和父类在同一个包下时,子类可以访问父类默认权限的属性,如果子类和父类不在同一个包下时,无法访问
*/
demo2 d=new demo2(15);
d.setId("1");

}
}
class demo2 extends OopDemo{
public demo2(int age) {
super(age);
}
}

get和set方法
package com.OnClass.day3.Demo;

public class User {

private String name;
private int age;
private int id;

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 getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", id=" + id +
'}';
}
}

package com.OnClass.day3.Demo;

public class App {

public static void main(String[] args) {
User user = new User();
user.setName("jim");
System.out.println(user.getName());
}
}


封装面试:
在日常封装哪些体现、怎么用封装、封装做过哪些设计,父类中私有属性和方法子类是否可以继承
getter&setter,无需对外暴露的方法即私有化,静态工厂方法of()
b.继承
1、继承好处:减少代码复用性、方便扩展。
2、在实例化子类时,父类也会被实例化。
3、子类是无法继承父类的私有属性,也无法直接访问父类的私有属性。但如果父类中有对私有属性的get和set的方法,而且是public的修饰的方法,子类在继承父类的同时,也继承了带有public修饰的set和get方法,所以可以通过以下方式子类可以访问到父类的私有属性。

package test;

class Person{
private String name;
public Integer age;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}


}

class Student extends Person{

}

public class Test {
public static void main(String[] args) {
Student s = new Student();

s.age = 12;
s.setName("小明");

String name = s.getName();
System.out.println("name = "+ name);

}
}
4、super()关键字使用
指向父类指针
作用:在子类中,调用父类的方法,属性,构造器
用法:super.name,super.run()
注意:super调用父类构造器必须在首行,因为先调用父类构造器
package com.OnClass.day3.Demo2;

public class Person {
private String name;

private int id;

int age;

public Person(String name, int id) {
this.name = name;
this.id = id;
}

void run(){
System.out.println("人有两条腿");
}

public static void main(String[] args) {
stu stu=new stu("小黑",20);
System.out.println(stu.age);
}
}

class stu extends Person{
public stu(String name, int id) {
super(name, id);
super.age=19;
super.run();
}
}

c.多态
instanceof关键字
语法:instanceof Class
使用场景:多态实现时,用于前置判断,避免类转换异常

父类:
package com.OnClass.day3.Demo2;

public class Car {
/**
* 品牌
*/
private String brand;
/**
* 排量
*/
private String output;
/**
* 速度
*/
private int speed;
/**
* 颜色
*/
private String clor;

public Car(String brand, String output) {
this.brand = brand;
this.output = output;
}

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}

public String getOutput() {
return output;
}

public void setOutput(String output) {
this.output = output;
}

public int getSpeed() {
return speed;
}

public void setSpeed(int speed) {
this.speed = speed;
}

public String getClor() {
return clor;
}

public void setClor(String clor) {
this.clor = clor;
}
}

子类:
package com.OnClass.day3.Demo2;

public class Baom extends Car {
public Baom() {
super("宝马", "4.0");
}
}

package com.OnClass.day3.Demo2;

public class Benc extends Car {
public Benc() {
super("奔驰", "1.0");
}
}

主方法:
package com.OnClass.day3.Demo2;
public class App {
public static void main(String[] args) {
Car car1=new Benc();
/**
* 提现多态的写法,是下面的run方法, run方法在面向的是一个抽象的Car
*
* 在run方法看来, 它只看Car,这个抽象的东西, 具体是宝马, 还是奔驰, run不在乎
*/
run(car1);
}


public static void run(Car car){
System.out.println("马路上"+car.getBrand()+"在跑");
}

 

}

 

(2)抽象类
1、定义抽象类,使用abstract关键字,不能实例化
2、定义一个抽象方法,使用abstract关键字,但是方法

package com.OnClass.day3.Demo3;

/**
* 测试抽象类
* 1、定义抽象类和抽象方法,都需要使用abstract关键字,方法不能实现,抽象类不能new
* 2、子类继承抽象类时必须实现抽象方法
*/

public abstract class Person {
private String type;

public Person(String type) {
this.type = type;
}
protected abstract void talk();

public String getType() {
return type;
}
}

子类:
package com.OnClass.day3.Demo3;

public class China extends Person {

public China() {
super("中国人");
}
@Override
protected void talk() {
System.out.println("人种"+this.getType()+"讲中文");
}
}

package com.OnClass.day3.Demo3;

public class English extends Person {

public English() {
super("英国人");
}
@Override
protected void talk() {
System.out.println("人种"+this.getType()+"讲英语");
}
}

入口:
package com.OnClass.day3.Demo3;

public class App {
public static void main(String[] args) {
Person p=new China();
speak(p);

}

public static void speak(Person p){
p.talk();
}

(3)接口

package com.OnClass.day3.Demo4;

public interface Person {
/**
* 抽象类的定义方式:protected abstract void talk();
*/

void talk();
}

实现接口的子类
package com.OnClass.day3.Demo4;

/**
* 1、子类实现接口,使用关键字implements
* 2、子类要实现接口中分方法
*/

public class Stu implements Person {
@Override
public void talk() {

}
}

 

推荐阅读