目录

封装是面向对象的核心思想,将对象的属性和行为封装起来,不需要让外界知道具体实现细节,这就是封装思想
1.封装是指在定义一个类时,将类中的属性私有化,即使用private关键字来修饰。
2.私有属性只能在它所在类中被访问,如果外界想要访问私有属性,需要提供一些使用public修饰的公有方法。


说明:
1.利用构造方法的重载可以实用多个构造方法
2.没有写构造方法,java虚拟机默认会生成一个没有任何参数的公共的构造方法。
3.相同的名称的普通方法也可以有多个,这些方法的参数列表必须不一样。
在同一个类中,多个方法中拥有相同的名字,但方法的参数列表不同(参数个数不同,顺序不同、类型不同)
- package oop.entity;
-
- /**
- *企鹅类
- * @author zyt
- */
- public class Penguin {
-
- private String name;//昵称
- private int health;//健康值
- private String sex;//性别
- public Penguin() {
- }
-
- public Penguin(String name, int health) {
- this.name = name;
- this.health = health;
- }
-
- /**
- *构造方法--带三个参数
- * @param name
- * @param health
- * @param sex
- */
- public Penguin(String name, int health, String sex) {
- this.name = name;
- this.health = health;
- this.sex = sex;
- }
-
-
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getHealth() {
- return health;
- }
-
- public void setHealth(int health) {
- this.health = health;
- }
-
- public String getSex() {
- return sex;
- }
-
- public void setSex(String sex) {
- this.sex = sex;
- }
- }
- package oop.entity;
-
- public class PenguinText {
- public static void main(String[] args) {
- //通过构造方法,将对象的属性值初始化
- Penguin penguin = new Penguin("Q仔",100,"男");
- // penguin.setName("Q仔");
- // penguin.setHealth(100);
- // penguin.setSex("男");
-
-
- System.out.println(penguin.getName()+","+ penguin.getHealth()+","+ penguin.getSex());
- }
- }

说明:
static表示“静态”的意思,用来修饰成员变量和成员方法,也可以形参静态 static代码块等
- package oop.entity;
-
- public class Person {
- String name;
- static String classes;
-
-
- public static void print(){
- System.out.println("班级" +classes +"的同学");
- }
- public void show(){
- System.out.println(name);
- }
- public void show(String str){
- System.out.println(name + "");
- }
- //return 不能作为重载依据
-
- public static void main(String[] args) {
- Person p1 = new Person();
- p1.name= "张三";
- // p1.classes="1001";
- Person.classes="1001";
-
- Person p2= new Person();
- p2.name= "李四";
- // p2.classes="1001";
-
- Person p3= new Person();
- p3.name= "王五";
- // p3.classes="1001";
-
- p1.print();
- p2.print();
- p3.print();
- }
- }
不管创建多少个对象,这个static修饰的变量,它只占一块内存
注:如果是一个静态的方法,那么它只能访问静态的属性。

格式 :
class 子类 extends 父类{
....
}

-
- public class Animal {
- private String name;
- private int id;
-
- public Animal(String name, int id) {
- this.name = name;
- this.id = id;
- }
- public void eat(){
- System.out.println(name+"正在吃");
- }
- public void sleep(){
- System.out.println(name+"正在睡");
- }
- public void introduction(){
- System.out.println("大家好!我是" + id +"号"+ name);
- }
- }
- package oop.entity;
-
- public class Mouse extends Animal{
- public Mouse(String myName,int myId){
- super(myName,myId);
- }
- }

单继承指一个子类只能继承一个父类,多继承指的是B类继承A类,A类是B的父类;C类继承B类,B类是C类的父类。

父类:
- package oop.entity;
-
- public class Fu {
- private int num = 4;
- public int getNum(){
- return this.num;
- }
- }
子类:
- package oop.entity;
-
- import java.sql.SQLOutput;
-
- public class Zi extends Fu{
- private int num= 5;
-
- public void show(){
- System.out.println(this.num+"...."+super.getNum());
- }
- }
测试类:
- package oop;
-
- import oop.entity.Zi;
-
- public class ZiText {
- public static void main(String[] args) {
- Zi z = new Zi();
- z.show();
- }
- }



Animal类:(父类)
- package oop.entity;
-
- public class Animal {
- private String name;
- private int id;
-
- public Animal(String name, int id) {
- this.name = name;
- this.id = id;
- }
- public void eat(){
- System.out.println(name+"正在吃");
- }
- public void sleep(){
- System.out.println(name+"正在睡");
- }
- public void introduction(){
- System.out.println("大家好!我是" + id +"号"+ name);
- }
- }
Mouth类:
- package oop.entity;
-
- public class Mouse extends Animal{
- public Mouse(String myName,int myId){
- super(myName,myId);
- }
-
- public void eat(){
- System.out.println("老鼠正在吃粮食");
- }
- }
测试类:
- package oop;
-
- import oop.entity.Mouse;
-
- public class MouseText {
- public static void main(String[] args) {
- Mouse m = new Mouse("Jerry",01);
- m.eat();
-
- }
- }
格式
- //抽象类
- 修饰符 abstract class 类名{
- //抽象方法
- 修饰符 abstract 返回值类型 方法名([参数列表]);
- }
说明:
abstract修饰方法,称为抽象方法,只有方法的定义,没有方法体并且只能定义在抽象类中;abstract 修饰类,称为抽象类,抽象类,抽象类不能直接实例化,需要借助子类重写类中全部的抽象方法,通过子类创建对象。
父类:(抽象类)
- package oop.entity;
-
- public abstract class Animal01 {
-
- private String name;
-
- public abstract void eat();
-
- }
子类:
- package oop.entity;
-
- public class Mouse01 extends Animal01{
-
- @Override
- public void eat() {
- System.out.println("老鼠在吃粮食");
- }
- }
- package oop.entity;
-
- public class Penguin01 extends Animal01{
-
- @Override
- public void eat() {
- System.out.println("企鹅正在吃鱼虾");
- }
- }
常见问题一:

常见问题二:

目的:不让这个类去创建对象
常见问题3:

常见问题4:



- package oop.entity;
-
- /**
- * final 修饰类不能被继承
- */
- public final class Msg {
-
- //定义常量(final+static 修饰属性)
- public static final String TITLE ="java";
-
- public final void show(){
- System.out.println(TITLE);
- }
-
-
- }
- package oop;
-
- import oop.entity.Msg;
-
- public class MsgText {
- public static void main(String[] args) {
-
-
- Msg m = new Msg();
- // m.TITLE; 会报错
- m.show();
-
- }
- }

概念
多态指允许不同类的对象对同一消息做出相应。
即同一消息可以根据发生对象的不同而采取不同的行为方式

抽象类
- package oop.ob;
-
- public abstract class Animal {
- abstract void eat();
- }
子类1:
- package oop.ob;
-
- public class Cat extends Animal {
-
- public void eat(){
- System.out.println("吃鱼");
- }
-
- public void catchMouse(){
- System.out.println("抓老鼠");
- }
- }
子类2:
- package oop.ob;
-
- import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
-
- public class Dog extends Animal{
-
- @Override
- public void eat() {
- System.out.println("啃骨头");
- }
- public void lookHome(){
- System.out.println("看家");
- }
- }
-
- package oop.ob;
-
- public class PolymorphicDemo {
-
-
- /**
- * 使用父类作为方法的参数实现多态
- * @param a
- */
- public static void method(Animal a){
- a.eat();
- }
-
- /**
- * 使用父类作为方法的返回值实现多态
- * @return
- */
- public static Animal get()
- {
- return new Dog();
- }
-
- public static void main(String[] args) {
-
- // //调用
- // Cat c = new Cat();
- // method(c);
- // method(new Dog());//Dog d = new Dog;
-
- //使用父类的引用指向子类对象
- Animal c = new Cat();
- // Animal d = new Dog();
- method(c);
- method(new Dog());
- }
- }
-
1.继承
2.重写
3.父类引用指向子类对象[Animal c = new Cat()]
当使用多态方法调用方式时,首先检查父类中是否有该方法,如果没有,则编译错误;如果有再去调用子类同名方法。

向上转型例:Animal a = new (Cat);
向下转型例:Cat c = (Cat)a; c.__();
- package oop.ob;
-
- public class PolymorphicDemo {
-
-
- /**
- * 使用父类作为方法的参数实现多态
- * @param a
- */
- public static void method(Animal a){
- //调用eat()方法
- a.eat();
-
- //instanceof 判断对象的具体方法
- //原先的 a.__是找不到子类特有的方法 先判断,接着向下转型 就可以调用子类特有的方法了
- if(a instanceof Cat){
- Cat c =(Cat) a;
- c.catchMouse();
- }else if(a instanceof Dog){
- Dog d =(Dog) a;
- d.lookHome();
- }
- }

接口的特点
特点:统一标准,让大家知道是做什么的,但是不知道具体怎么做。

接口中的属性默认是使用 public static final修饰的 修饰的是常量
接口中的默认方法是使用 public abstract 修饰的


use 3.0继承了 usb2.0b并拓展了


注:多继承


注:多实现

接口1:
- package oop.service;
-
- /**
- * 接口:提供一种公共的功能
- */
- public interface MathService01 {
-
- public int sum(int a,int b);//+
-
- public int sub(int a,int b);//-
-
- }
接口2:
- package oop.service;
-
- /**
- * 接口:提供一种公共的功能
- */
- public interface MathService02 {
- public int mul(int a,int b);//*
-
- public int div(int a,int b);// /
- }
实现:
- package oop.service.impl;
-
- import oop.service.MathService01;
- import oop.service.MathService02;
-
- public class MathServiceImpl implements MathService01, MathService02 {
-
- @Override
- public int sub(int a, int b) {
- return a-b;
- }
-
- @Override
- public int sum(int a, int b) {
- return a+b;
- }
-
- @Override
- public int div(int a, int b) {
- return a/b;
- }
-
- @Override
- public int mul(int a, int b) {
- return a*b;
- }
- }
测试类:
- package oop.service;
-
-
- import oop.service.impl.MathServiceImpl;
-
- public class MathText {
- public static void main(String[] args) {
-
- MathService01 ms1 = new MathServiceImpl();
- ms1.sub(10,10);
- ms1.sum(10,10);
-
- MathService02 ms2 = new MathServiceImpl();
- ms2.div(10,10);
- ms2.mul(10,10);
-
-
-
- }
- }