zoukankan      html  css  js  c++  java
  • 面向对象的三大特征之一继承

    继承的理解

    1、继承是单继承,继承一个父类
    2、判断子类与父类的关系:is-a(狗是一个宠物)
    3、用关键词extends
    4子类可以调用父类的方法(注有权限问题)

    子类访问父类的成员
    1访问父类的构造方法
    super();
    super(name);
    注:在子类的构造方法中必须是第一句
    2访问父类的属性
    super.name;
    3访问父类的方法
    super.print()
    注 :使用super关键字。super代表父类的对象,super可以省略。super只能出现在子类的方法中和构造方法中,不能访问父类的private成员
    访问修饰符的总结        有无访问权限
    访问修饰符              本类                     同包                    子类                     其他
    private                    有                         无                         无                       无
    默认(friendly)     有                         有                         无                       无
    protect                    有                         有                         有                      无
    public                      有                         有                         有                      有
    三何时使用继承
    符合is-a的关系的设计使用继承
    代码
    父类的代码
    package com.yt.extend;

    public class Furit {
        private String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        private String color;
        private String size;
        private String shape;
        
        public String getColor() {
            return color;
        }
        public void setColor(String color) {
            this.color = color;
        }
        public String getSize() {
            return size;
        }
        public void setSize(String size) {
            this.size = size;
        }
        public String getShape() {
            return shape;
        }
        public void setShape(String shape) {
            this.shape = shape;
        }
        
        public String show(){
            String msg=name+"的颜色是"+color+"大小是"+size+"形状是"+shape;
            return msg;
        }
    }
    子类的代码
    public class Apple extends Furit {
        private   String  taste;

        public String getTaste() {
            return taste;
        }

        public void setTaste(String taste) {
            this.taste = taste;
        }
        
        
        public Apple(){
            setName("苹果");
            setShape("圆的");
            setColor("红色");
            setSize("5英尺");
            taste="甜甜的";
            
        }
        
        public String show(){
            String msg=getName()+"的颜色是"+getColor()+"大小是"+getSize()+"形状是"+
            getShape()+"味道是"+taste;
            return msg;
            
            
        }
    }
    测试类的代码:
       public class Test {
       public static  void main(String[]args){
           Apple fu=new Apple();
           String result=fu.show();
           System.out.println(result);
           
           Grape fi=new Grape();
           String results=fi.show();
           System.out.println(results);
           
       }
    }
    子类的方法和父类的方法是重写的关系
    方法重写的定义:
    方法名相同
    参数列表相同
    返回类型相同
    是其子类
    访问权限不得小于父类
    注:子类不能重写父类的构造方法

  • 相关阅读:
    Centos 7 zabbix 实战应用
    Centos7 Zabbix添加主机、图形、触发器
    Centos7 Zabbix监控部署
    Centos7 Ntp 时间服务器
    Linux 150命令之查看文件及内容处理命令 cat tac less head tail cut
    Kickstart 安装centos7
    Centos7与Centos6的区别
    Linux 150命令之 文件和目录操作命令 chattr lsattr find
    Linux 发展史与vm安装linux centos 6.9
    Linux介绍
  • 原文地址:https://www.cnblogs.com/itchenguo/p/10974476.html
Copyright © 2011-2022 走看看