第二章:面向对象
一、对象的封装
1. 类和对象
1.1 类
1.2 对象
1.3 对象的属性
1.4 对象的行为
1.5 类和对象的关系
1.6 类的定义
//类
public class Phone(){
//成员变量
String brand;
int price;
//成员方法
public void call(){
System.out.println{"打电话"};
}
public void sendMessage(){
System.out.println("发短信");
}
}
1.9 对象的使用
//注意对象和类是结合使用的
public class PhoneDemo(){
public static void main(String[] args){
//-----------------成员变量的使用
//创建对象
Phone p = new Phone();
//使用对象
System.out.println(p.brand);
System.out.println(p.price);
//变量赋值
p.brand = "小米";
p.price = 59999;
System.out.println(p.brand);
System.out.println(p.price);
//------------------成员方法的使用
//使用成员方法
p.call();
p.sendMessage();
}
}
2.0 案例学生类的定义和使用
//--------------------------------------------------学生类
package com.iteima_01;
public class Student {
//成员变量
String name;
int age;
//成员方法
public void study(){
System.out.println("好好学习,天天向上");
}
public void doHomework(){
System.out.println("键盘敲烂,月薪过万");
}
}
//--------------------------------------------------学生类的使用
package com.iteima_01;
//学生测试类
public class StudentDemo {
public static void main(String[] args){
//创建对象
Student s = new Student();
//使用对象
System.out.println(s.name+","+s.age);
s.name = "马云";
s.age = 44;
System.out.println(s.name+","+s.age);
//使用方法
s.study();
s.doHomework();
}
}
2.1 成员变量和局部变量的含义
2.2 成员变量和局部变量的区别
二、对象的继承
1. 继承
1. 案例
//--------------------------------------这是父类
package com.iteima_01;
public class Fu {
public void call(){
System.out.println("Fu方法被调用");
}
}
//--------------------------------------这是子类
package com.iteima_01;
public class Zi extends Fu{
public void test(){
System.out.println("这是Zi方法");
}
}
//--------------------------------------这是main方法测试类
package com.iteima_01;
public class Demo {
public static void main(String[] args) {
Fu f = new Fu();
Zi z = new Zi();
f.call();
z.test();
z.call();
}
}
2. 继承的好处和弊端
3. 继承中变量的访问特点
1. 继承中变量的访问顺序
2. 继承中 this super 的使用方法
//--------------------------------------这是父类
package com.iteima_01;
public class Fu {
int a = 30;
public void call(){
System.out.println("Fu方法被调用");
}
}
//--------------------------------------这是子类
package com.iteima_01;
int a = 20;
public class Zi extends Fu{
public void test(){
int a = 10;
System.out.println("这是访问局部变量a"+a);
System.out.println("这是访问子类变量a"+this.a);
System.out.println("这是访问父类变量a"+super.a);
}
}
3. 继承中构造方法的访问特点
4. 继承中成员方法的访问特点
5. 方法重写
//--------------------------------------这是父类
package com.iteima_01;
public class Fu {
public void call(String name){
System.out.println("给"+name+"打电话");
}
}
//--------------------------------------这是子类
package com.iteima_01;
public class Zi extends Fu{
@Override
public void call(String name){
System.out.println("开启视频通话功能");
super.call(name);
}
}
//--------------------------------------这是main方法测试类
package com.iteima_01;
public class Demo {
public static void main(String[] args) {
Fu f = new Fu();
f.call("马云");
System.out.println("---------");
Zi z = new Zi();
z.call("马云");
}
}
- 私有方法不能被重写(父类的私有成员子类是不能被继承的)
- 方法重写的时候,子类的方法权限不能低于父类(public > 默认 > 私有)
6. Java继承的注意事项
7. 案例 老师和学生 super传参
```java //——————————————————————————人类 package com.iteima_01;
public class People { private String name; private int age;
public People() {
}
public People(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} //——————————————————————————老师类 package com.iteima_01;
public class Teacher extends People { public Teacher(){}
public Teacher(String name,int age) {
super(age, name);
}
public void teach(){
System.out.println("用爱成就每一位学员");
}
} //——————————————————————————学生类 package com.iteima_01;
public class Student extends Teacher{ public Student(){} public Student(String name,int age){ super(name, age); } public void study(){ System.out.println(“好好学习,天天向上”); } } //——————————————————————————测试类 package com.iteima_01;
public class Demo { public static void main(String[] args) { //无参老师类 Teacher t1 = new Teacher(); t1.setName(“武当王也”); t1.setAge(18); System.out.println(t1.getName()+”,”+t1.getAge()); t1.teach();
//带参老师类
Teacher t2 = new Teacher("校长",20);
System.out.println(t2.getName()+","+t2.getAge());
t2.teach();
System.out.println("-------------------我是分割线-----------------------");
//无参学生类
Student s1 = new Student();
s1.setName("全性龚庆");
s1.setAge(15);
System.out.println(s1.getName()+","+s1.getAge());
s1.study();
//带参学生类
Student s2 = new Student("学生",13);
System.out.println(s2.getName()+","+s2.getAge());
s2.study();
}
}
<a name="oLZSi"></a>
#### 8. 案例 猫和狗 super传参
```java
//----------------------------------------------------动物类
package com.iteima_01;
//这是动物类
public class People {
private String name;
private int age;
public People() {
}
public People(String name, int age) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//----------------------------------------------------猫类
package com.iteima_01;
//这是猫类
public class Teacher extends People{
public Teacher(){}
public Teacher(String name,int age){
super (name,age);
}
public void cat() {
System.out.println("我会抓老鼠");
}
}
//----------------------------------------------------测试类
package com.iteima_01;
public class Demo {
public static void main(String[] args) {
//无参猫类
Teacher t1 = new Teacher();
t1.setName("黑猫");
t1.setAge(6);
System.out.println(t1.getName()+","+t1.getAge());
t1.cat();
//带参猫类
Teacher t2 = new Teacher("白猫",8);
System.out.println(t2.getName()+","+t2.getAge());
t2.cat();
}
}
4. package 包
1. 包的概述和创建
2. 导包
3. 权限修饰符 默认、private、protected、public
package com.iteima_01;
public class People{
private void show1(){
System.out.println("private");
}
void show2(){
System.out.println("默认");
}
protected void show3(){
System.out.println("protected");
}
public void show4(){
System.out.println("public");
}
}
4. final修饰局部变量
package com.iteima_01;
public class Demo {
public static void main(String[] args) {
//final修饰局部变量
final int a = 10;
System.out.println("这是赋值前局部变量"+a);
// a = 100;
System.out.println("这是赋值后局部变量"+a);
//final修饰引用变量
final Teacher t = new Teacher();
System.out.println("这是赋值前引用变量"+t.a);
t.a = 200;
System.out.println("这是赋值后引用变量"+t.a);
// t = new Teacher();
}
}
5. static
package com.iteima_01;
public class Teacher {
public String name;
public int age;
public static String from;
public void show(){
System.out.println(name+","+age+","+from);
}
}
//----------------------------------------------------测试类
package com.iteima_01;
public class Demo {
public static void main(String[] args) {
Teacher.from = "China";
Teacher t = new Teacher();
t.name = "张三";
t.age = 20;
// t.from = "China";
t.show();
Teacher t2 = new Teacher();
t2.name = "李四";
t2.age = 22;
// t.from = "China";
t2.show();
}
}
6. static的访问特点
- 静态成员方法只能访问静态成员
- 非静态成员能访问所有的成员
5. 多态
1. 多态的简介案例
```java //——————————————————————————父类 package com.iteima_01;
public class Fu { public void eat(){ System.out.println(“我是父类猫”); } } //——————————————————————————子类 package com.iteima_01;
public class Zi extends Fu{ @Override public void eat() { System.out.println(“子类的猫会吃鱼”); } } //——————————————————————————测试类 package com.iteima_01;
public class Demo { public static void main(String[] args) { Fu f = new Zi(); f.eat(); } }
<a name="dBy1t"></a>
#### 2. 多态的访问特点
![image.png](https://cdn.nlark.com/yuque/0/2021/png/5358492/1618149595818-34ce2773-3a0a-4686-a8d6-8d1315eb06ae.png#align=left&display=inline&height=163&id=lmRbU&margin=%5Bobject%20Object%5D&name=image.png&originHeight=163&originWidth=382&size=57136&status=done&style=none&width=382)
<a name="SwCcu"></a>
#### 3. 多态的好处和弊端
![image.png](https://cdn.nlark.com/yuque/0/2021/png/5358492/1618150103112-cb4d9383-9115-42d0-ad22-5aa3edb3e39c.png#align=left&display=inline&height=90&id=lkdGu&margin=%5Bobject%20Object%5D&name=image.png&originHeight=90&originWidth=820&size=52785&status=done&style=none&width=820)
<a name="ACeXY"></a>
#### 4. 多态的转型
![image.png](https://cdn.nlark.com/yuque/0/2021/png/5358492/1618150308544-e152a7d4-db45-49a5-80d7-f32b8469cc80.png#align=left&display=inline&height=194&id=kmz8g&margin=%5Bobject%20Object%5D&name=image.png&originHeight=194&originWidth=229&size=27994&status=done&style=none&width=229)
```java
//----------------------------------------------------父类
package com.iteima_01;
public class Fu {
public void eat(){
System.out.println("我是父类猫");
}
}
//----------------------------------------------------子类
package com.iteima_01;
public class Zi extends Fu{
@Override
public void eat() {
System.out.println("子类的猫会吃鱼");
}
public void game(){
System.out.println("子类的猫会玩游戏");
}
}
//----------------------------------------------------测试类
package com.iteima_01;
public class Demo {
public static void main(String[] args) {
//向上转型
Fu f = new Zi();
f.eat();
//向下转型
Zi z = (Zi) f;
z.eat();
z.game();
}
}
5. 案例 猫和狗 多态继承的方法
//-------------------------------------------------父类
package com.iteima_01;
public class Fu {
private String name;
private int age;
public Fu() {}
public Fu(String name,int age){
this.name = name;
this.age = age;
}
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 void eat(){
System.out.println("父类吃饭");
}
}
//-------------------------------------------------子类
package com.iteima_01;
public class Zi extends Fu{
public Zi() {
}
public Zi(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("子类的猫会玩游戏");
}
}
//-------------------------------------------------测试类
package com.iteima_01;
public class Demo {
public static void main(String[] args) {
Fu f = new Zi();
f.setAge(5);
f.setName("黑猫");
System.out.println(f.getName()+","+f.getAge());
f.eat();
f = new Zi("白猫",4);
System.out.println(f.getName()+","+f.getAge());
f.eat();
}
}
6. 抽象类 abstract
7. 抽象类的特点
8. 抽象类成员的特点
9. 案例 猫和狗 抽象类的使用
//-------------------------------------------------父类
package com.iteima_01;
public abstract class Fu {
private String name;
private int age;
public Fu() {
}
public Fu(String name, int age) {
this.name = name;
this.age = age;
}
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 abstract void eat();
}
//-------------------------------------------------子类
package com.iteima_01;
public class Zi extends Fu{
public Zi() {
}
public Zi(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("子类的猫会晒太阳");
}
}
//-------------------------------------------------测试类
package com.iteima_01;
public class Demo {
public static void main(String[] args) {
Fu f = new Zi();
f.eat();
f.setName("黑猫");
f.setAge(4);
System.out.println(f.getName()+","+f.getAge());
f = new Zi("白猫",5);
System.out.println(f.getName()+","+f.getAge());
f.eat();
}
}
6. 接口 interface implements
1.描述
2. 接口的特点
3. 接口成员的特点
4. 案例 猫和狗 接口的使用
//------------------------------------------------------主类
package com.iteima_02;
public interface Jumpping{
public abstract void jump();
}
//------------------------------------------------------动物类
package com.iteima_02;
public abstract class Animal {
private String name;
private int age;
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 Animal(String name, int age) {
this.name = name;
this.age = age;
}
public Animal() {
}
public abstract void eat();
}
//------------------------------------------------------猫类
package com.iteima_02;
public class Cat extends Animal implements Jumpping{
@Override
public void eat() {
System.out.println("猫吃鱼");
}
@Override
public void jump() {
System.out.println("猫会跳高");
}
public Cat(String name, int age) {
super(name, age);
}
public Cat() {
}
}
//------------------------------------------------------测试类
package com.iteima_02;
public class Demo {
public static void main(String[] args) {
Jumpping j = new Cat();
j.jump();
Animal a = new Cat();
a.setName("黑猫");
a.setAge(6);
System.out.println(a.getName()+","+a.getAge());
a.eat();
a = new Cat("白猫",4);
System.out.println(a.getName()+","+a.getAge());
a.eat();
}
}
5. 类和接口的关系
6. 抽象类和接口的区别
7. 案例 运动员和教练
//------------------------------------------------------------------------学英语接口
package Demo;
public interface LeanEnglish {
public void speakEnglish();
}
//------------------------------------------------------------------------人总类
package Demo;
public abstract class Ren {
private int age;
private String name;
public Ren(int age, String name) {
this.age = age;
this.name = name;
}
public Ren() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void each(){
System.out.println("我是人类吃饭");
}
}
//------------------------------------------------------------------------学生类
package Demo;
public abstract class Study extends Ren{
public Study() {
}
public Study(int age, String name) {
super(age, name);
}
public void xue(){
System.out.println("我是学生类");
}
}
//------------------------------------------------------------------------老师类
package Demo;
public abstract class Teach extends Ren{
public Teach(int age, String name) {
super(age, name);
}
public Teach() {
}
public void jiao(){
System.out.println("我是老师类");
}
}
//------------------------------------------------------------------------篮球老师类
package Demo;
public class Lteacher extends Teach{
public Lteacher(int age, String name) {
super(age, name);
}
public Lteacher() {
}
@Override
public void jiao() {
System.out.println("我是篮球教练,我教悠悠球");
}
@Override
public void each() {
System.out.println("我是篮球教练,我吃奶");
}
}
//------------------------------------------------------------------------乒乓球老师类
package Demo;
public class Pteacher extends Teach implements LeanEnglish{
public Pteacher(int age, String name) {
super(age, name);
}
public Pteacher() {
}
@Override
public void each() {
System.out.println("我是乒乓球教练,我教国足");
}
@Override
public void jiao() {
System.out.println("我是乒乓球教练,我吃肉");
}
@Override
public void speakEnglish() {
System.out.println("我是乒乓球教练,我教英语");
}
}
//------------------------------------------------------------------------篮球学生类
package Demo;
public class Lstudent extends Study{
public Lstudent() {
}
public Lstudent(int age, String name) {
super(age, name);
}
@Override
public void each() {
System.out.println("我是篮球运动员,我吃屎");
}
@Override
public void xue() {
System.out.println("我是篮球运动员,我学打篮球");
}
}
//------------------------------------------------------------------------乒乓球学生类
package Demo;
public class Pstudent extends Study implements LeanEnglish{
public Pstudent() {
}
public Pstudent(int age, String name) {
super(age, name);
}
@Override
public void each() {
System.out.println("我是乒乓球运动员,我吃求");
}
@Override
public void xue() {
System.out.println("我是乒乓球运动员,我学打乒乓球");
}
@Override
public void speakEnglish() {
System.out.println("我是乒乓球运动员,我学习说英语");
}
}
//------------------------------------------------------------------------测试类
package Demo;
//测试类
public class Demo {
public static void main(String[] args) {
//测试乒乓球
Pstudent p = new Pstudent();
p.setName("李四");
p.setAge(20);
System.out.println(p.getName()+","+p.getAge());
p.xue();
p.each();
p.speakEnglish();
System.out.println("----------------------------");
Pteacher p2 = new Pteacher();
p2.setAge(30);
p2.setName("曹大大");
System.out.println(p2.getName()+","+p2.getAge());
p2.jiao();
p2.each();
p2.speakEnglish();
}
}
7. 内部类
1. 类名作为形参和返回值
2. 抽象类名作为形参和返回值
3. 接口名作为形参和返回值
4. 内部类访问特点
- 内部类可以直接访问外部类的成员,包括私有
- 外部类想要访问内部类的成员,必须先创建对象
5. 成员内部类
```java //——————————————————————————————————————主类 package com;
public class Outer { int num = 20; / 第一种 public class Inner{ public void show () { System.out.println(num); } } /
private class Inner{
public void show() {
System.out.println(num);
}
}
public void method() {
Inner i = new Inner();
i.show();
}
} //——————————————————————————————————————测试类 package com;
public class Demo { / 第一种 public static void main(String[] args) { Outer.Inner i = new Outer().new Inner(); i.show(); } / public static void main(String[] args) { Outer out = new Outer(); out.method(); } }
<a name="VVN0k"></a>
#### 6. 局部内部类
![image.png](https://cdn.nlark.com/yuque/0/2021/png/5358492/1618405405099-c58cb979-6905-4ed9-8850-f87777deb3d4.png#align=left&display=inline&height=62&id=VFJv4&margin=%5Bobject%20Object%5D&name=image.png&originHeight=62&originWidth=736&size=48382&status=done&style=none&width=736)
```java
//----------------------------------------------------------------------------主类
package com;
public class Outer {
private int num = 10;
public void method(){
int a = 30;
class Inner{
public void show(){
System.out.println(num);
System.out.println(a);
}
}
Inner i = new Inner();
i.show();
}
}
//----------------------------------------------------------------------------测试类
package com;
public class Demo {
public static void main(String[] args) {
Outer o = new Outer();
o.method();
}
}
7. 匿名内部类
//----------------------------------------------------------------------------主类
package com;
public class Outer {
public void method() {
inter i = new inter(){
@Override
public void show() {
System.out.println("内部接口调用");
}
};
for (int a=0;a<5;a++){
i.show();
}
}
}
//----------------------------------------------------------------------------接口类
package com;
public interface inter {
void show();
}
//----------------------------------------------------------------------------测试类
package com;
public class Demo {
public static void main(String[] args) {
Outer o = new Outer();
o.method();
}
}
8. 匿名内部类在开发中的使用
//----------------------------------------------------------------------------主类
package com;
public class Outer {
public void Method(inter j){
j.jump();
}
}
//----------------------------------------------------------------------------接口
package com;
public interface inter {
void jump();
}
//----------------------------------------------------------------------------测试类
package com;
import java.lang.reflect.Method;
public class Demo {
public static void main(String[] args) {
Outer o = new Outer();
o.Method(new inter(){
@Override
public void jump() {
System.out.println("猫可以跳高了");
}
});
}
}