ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java06面向对象02

2021-09-08 15:35:07  阅读:221  来源: 互联网

标签:Java06 02 mingmao void javaobjectoriented programming 面向对象 com public


Java06面向对象02

封装

image

package com.mingmao.javaobjectoriented.programming;

/*
封装的意义:
提高安全性
隐藏代码实现细节
统一接口,形成规范
提高系统的可维护性
 */
//学生类
public class StudentEncapsulation {
    //封装主要是封装属性,方法封装用的很少,private
    //属性私有,只有本类中可使用
    private String name;  //姓名
    private int id;    //学号
    private char sex;    //性别
    private int age;    //年龄

    //获取姓名
    public String getName(){
        return this.name;
    }
    //设置姓名
    public void setName(String name){
        this.name=name;
    }

    public int getId() {
        return id;
    }

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

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age>130 || age<0){
            System.out.println("您输入的年龄有误,请重新输入");
        }else{
            this.age = age;
        }
    }
}
package com.mingmao.javaobjectoriented.programming;

public class Application {
    public static void main(String[] args) {
        StudentEncapsulation student=new StudentEncapsulation();

        student.setName("mingmao");
        System.out.println(student.getName());

        student.setAge(200);

    }
}

继承的概念

image
查看继承树:Navigat-Type Hierarchy

package com.mingmao.javaobjectoriented.programming.inherit;

/*
修饰符:
public:跨包,跨类
protected:本类,本包,不同包的子类
default:默认,可以不写,同一个包内可访问
private:只有本类可访问
 */
//在Java中,都直接或间接默认继承Object类
//Java中只有单继承,没有多继承
//父类
public class Person {
    //public int money=10_0000_0000;
    private int money=10_0000_0000;

    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }

    public void say(){
        System.out.println("说了一句话");
    }
}
package com.mingmao.javaobjectoriented.programming.inherit;

//继承,子类
//子类继承了父类,会拥有父类所有方法
public class Student extends Person {
}
package com.mingmao.javaobjectoriented.programming.inherit;

//继承,子类
public class Teacher extends Person{
}
package com.mingmao.javaobjectoriented.programming;

import com.mingmao.javaobjectoriented.programming.inherit.Student;

public class Application {
    public static void main(String[] args) {

        Student student=new Student();
        System.out.println(student.getMoney());
        student.say();
    }
}

继承-super详解

image

package com.mingmao.javaobjectoriented.programming.inherit1;

public class Person {
    protected String name="mingmao";

    public Person() {
        System.out.println("person无参构造器执行了");
    }

    protected void print(){
        System.out.println("person");
    }

}
package com.mingmao.javaobjectoriented.programming.inherit1;

public class Student extends Person {
    private String name="mingmao1";

    public Student() {
        //隐藏代码,默认调用了父类的无参构造
        // super(); //调用父类的构造方法
        System.out.println("student无参构造器执行了");
    }

    public void printMethod(){
        print(); //调用本类的print方法
        this.print();//调用本类的print方法
        super.print();//调用父类的print方法
    }

    public void printName(String name){
        System.out.println(name);//传参
        System.out.println(this.name);//本类属性
        System.out.println(super.name);//父类属性
    }

    protected void print(){
        System.out.println("student");
    }
}
package com.mingmao.javaobjectoriented.programming;

import com.mingmao.javaobjectoriented.programming.inherit1.Student;

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

        /*
        person无参构造器执行了
        student无参构造器执行了
         */
        student.printName("mingmao2");
        /*
        mingmao2
        mingmao1
        mingmao
         */

        student.printMethod();
        /*
        student
        student
        person
         */
    }
}

继承-方法重写

提前了解多态

package com.mingmao.javaobjectoriented.programming.inherit2;

public class B {
    public static void test(){
        System.out.println("B=>test");
    }
}
package com.mingmao.javaobjectoriented.programming.inherit2;

//重写都是方法的重写,和属性无关
public class A extends B {

    public static void test(){
        System.out.println("A=>test");
    }
}
package com.mingmao.javaobjectoriented.programming;

import com.mingmao.javaobjectoriented.programming.inherit2.A;
import com.mingmao.javaobjectoriented.programming.inherit2.B;

public class Application {
    public static void main(String[] args) {

        A a=new A();
        a.test();//A=>test

        B b=new B();
        b.test();//B=>test

        //父类的引用指向了子类
        B b1=new A();
        b1.test();//B=>test

        //方法的调用只和左边的类型有关
    }
}

方法重写

上述基础上去掉static
方法重写快捷生成:右键Generate-Override-Methods...
image

package com.mingmao.javaobjectoriented.programming.inherit3;

public class B {

    public void test(){
        System.out.println("B=>test");
    }
}
package com.mingmao.javaobjectoriented.programming.inherit3;

public class A extends B {

    @Override //此为自动生成的注解,有功能
    public void test() {
        System.out.println("A=>test");
    }
}
package com.mingmao.javaobjectoriented.programming;

import com.mingmao.javaobjectoriented.programming.inherit3.A;
import com.mingmao.javaobjectoriented.programming.inherit3.B;

public class Application {
    public static void main(String[] args) {
        A a=new A();
        a.test();//A=>test

        B b=new B();
        b.test();//B=>test

        B b1=new A();
        b1.test();//A=>test

        /*
        静态方法与非静态方法有很大区别:
        静态方法:与类一起加载,方法的调用只与左边类型有关
        非静态方法:可以重写,方法的调用与右边new的对象有关
         */
    }
}

多态

image
父类引用指向子类
父类只能调用父类中有的方法
如果父类中的方法被子类重写则执行子类的方法

image

package com.mingmao.javaobjectoriented.programming.polymorphic;

public class Person {
    public void run(){
        System.out.println("run=>person");
    }
}
package com.mingmao.javaobjectoriented.programming.polymorphic;

public class Student extends Person{
    @Override
    public void run() {
        System.out.println("run=>student");
    }

    public void eat(){
        System.out.println("eat=>student");
    }
}
package com.mingmao.javaobjectoriented.programming;

import com.mingmao.javaobjectoriented.programming.polymorphic.Person;
import com.mingmao.javaobjectoriented.programming.polymorphic.Student;

public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        //new Student;
        //new Person;

        //但是可以指向的引用类型就不确定了
        //父类的引用指向子类
        Student s1=new Student();
        Person s2=new Student();
        Object s3=new Student();

        s1.run();
        s2.run();
        s1.eat();
        //s2.eat();出错

        //总结
        Person p=new Student();

        //父类引用指向子类
        //父类只能调用父类中有的方法
        //如果父类中的方法被子类重写则执行子类的方法
        p.run();

    }
}

学习视频

学习视频

标签:Java06,02,mingmao,void,javaobjectoriented,programming,面向对象,com,public
来源: https://www.cnblogs.com/mingmao/p/15242056.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有