码迷,mamicode.com
首页 > 编程语言 > 详细

java 基础-继承

时间:2016-09-21 09:02:10      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

1.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表。然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法showB输出大写的英文字母表。最后编写主类C,在主类的main方法中测试类A与类B。

//父类
public class Zimubiao {
    void showA()
    {
        System.out.println("abcd");
    }
}
//子类
public class Zimubiao2 extends Zimubiao {
    void showB()
    {
        System.out.println("ABCD");
    }
}
//测试
Zimubiao a =new Zimubiao();
        a.showA();
        Zimubiao2 b = new Zimubiao2();
        b.showA();
        b.showB();

技术分享

2.实现一个名为Person的类和它的子类EmployeeEmployee有两个子类Faculty和Staff。

具体要求如下:

1Person类中的属性有:姓名nameString类型),地址addressString类型),电话号码telphone(String类型)和电子邮件地址email(String类型);

(2)Employee类中的属性有:办公室office(String类型),工资wage(double类型),受雇日期hiredate(String类型);

(3)Faculty类中的属性有:学位degree(String类型),级别level(String类型);

(4)Staff类中的属性有:职务称号duty(String类型)。

//练习继承的关系

public class Person {
    String name;
    String address;
    String telephone;
    String email;
    
}
public class Employee extends Person {
                String office;
                double wage;
                String hiredate;
}
public class Faculty extends Employee {
        String degree;
        String level;
}
public class Staff extends Employee{
        String duty;
}

3.编写一个Car类,具有String类型的属性品牌,具有功能drive

定义其子类AodiBenchi,具有属性:价格、型号;具有功能:变速;

定义主类E,在其main方法中分别创建AodiBenchi的对象并测试对象的特性。

public class Car {
    String peipai;
    void dirver()
    {
        
    }
}
public class Aodi extends Car{
    double price;
    String xinghao;
    double biansu;
     public Aodi(double price,String xinghao,double biansu)
     {
         this.xinghao =xinghao;
         this.price =price;
         this.biansu =biansu;
     }
     double biansu1(int jia)
     {
         return biansu+=jia;
        
     }
     double biansu2(int jian)
     {
         return biansu-=jian;
        
     }

}
public class Benchi extends Car {
    double price;
    String xinghao;
    double biansu;
     public Benchi(double price,String xinghao,double biansu)
     {
         this.xinghao =xinghao;
         this.price =price;
         this.biansu =biansu;
     }
     double biansu1(int jia)
     {
         return biansu+=jia;
        
     }
     double biansu2(int jian)
     {
         return biansu-=jian;
        
     }
public class E {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Aodi a =new Aodi(200000.0,"AodiA8",150);
        a.peipai="Aodi";
        System.out.println("Aodi的属性"+"品牌为:"+a.peipai+" "+" 型号"+a.xinghao+" "+"价格"+a.price+" "+"特性"+a.biansu1(20));
        Benchi b =new Benchi(300000.0,"Benchi23",164);
        b.peipai ="Benchi";
        System.out.println("Benchi的属性"+"品牌为:"+b.peipai+" "+" 型号"+b.xinghao+"价格"+b.price+" "+"特性"+b.biansu2(20));
    }

}

技术分享

4.按要求编写一个Java应用程序:

1)编写一个矩形类Rect,包含:

   两个属性:矩形的宽width;矩形的高height

两个构造方法:

1.一个带有两个参数的构造方法,用于将widthheight属性初化;

2.一个不带参数的构造方法,将矩形初始化为宽和高都为10

两个方法:

求矩形面积的方法area()

求矩形周长的方法perimeter()

2)通过继承Rect类编写一个具有确定位置的矩形类PlainRect,其确定位置用

矩形的左上角坐标来标识,包含:

添加两个属性:矩形左上角坐标startXstartY

两个构造方法:

4个参数的构造方法,用于对startXstartYwidthheight属性初始化;

不带参数的构造方法,将矩形初始化为左上角坐标、长和宽都为0的矩形;

添加一个方法:

判断某个点是否在矩形内部的方法isInside(double x,double y)。如在矩形内,返回true, 否则,返回false。

  提示:点在矩形类是指满足条件:

x>=startX&&x<=(startX+width)&&y<startY&&y>=(startY-height)

3)编写PlainRect类的测试程序

创建一个左上角坐标为(1010),长为20,宽为10的矩形对象;

计算并打印输出矩形的面积和周长;

判断点(25.513)是否在矩形内,并打印输出相关信息。

public class Rect {
        double width;
        double height;
        public Rect(double width,double height)
        {
            this.width = width;
            this.height = height;
        }
        public Rect()
        {
            this.width=10;
            this.height=10;
        }
        public double area()
        {
            return  width*height;
        }
        public double perimeter()
        {
            return (width+height)*2;
        }
}

 

public class PlainRect extends Rect {
    double startX;
    double startY;
    public PlainRect(double startX,double startY,double width,double height)
    {
        this.height=height;
        this.width =width;
        this.startX =startX;
        this.startY=startY;
    }
    public PlainRect()
    {
        this.height=0;
        this.width=0;
        this.startX=0;
        this.startY=0;
    }
    public boolean isInside(double x,double y)
    {
        if(x>=startX&&x<=(startX+width)&&y<startY&&y>=(startY-height))
        {
            System.out.println("在矩形内");
            return true;
        }
        else
        {
            System.out.println("不在矩形内");
            return false;
        }
    }
}
public class TestPlainRect {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        PlainRect a =new PlainRect(10,10,10,20);
        System.out.println("矩形的左上角的坐标为"+"("+a.startX+","+a.startY+")");
        System.out.println("矩形的长和宽分别为:"+a.width+" "+a.height);
        System.out.println("矩形的面积为"+a.area());
        System.out.println("矩形的周长为"+a.perimeter());
        System.out.println(a.isInside(25.5, 13));
    }

}

技术分享

 

java 基础-继承

标签:

原文地址:http://www.cnblogs.com/miracle-0807/p/5891300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!