Java 面向对象 习题(高级篇)

个人主页:亮点的博客
个人信条:理想如果不向现实做一点点屈服,那么理想也将归于尘土
刷题专栏:【Java】牛客网刷题
刷题网站:牛客网 学习的一种有效途径就是刷题,丰富的做题经验可以加深对知识点的理解,推荐一款刷题网站,赶快点击这里注册学习吧你的进阶之路!


目录

  • 1、设计类Company:获取类信息
  • 2、编写Graph类
  • 3、建立一个人类(Person)和学生类(Student)
  • 4、定义员工类
  • 5、使用面向对象的概念表示出下面的生活场景


1、设计类Company:获取类信息

题目定义一个ClassName接口,接口中只有一个抽象方法getClassName();设计一个类Company,该类实现接口ClassName中的方法getClassName(),功能是获取该类的类名称;编写应用程序使用Company类。

代码如下:

interface ClassName{
    String getClassName();
}
class Company implements ClassName{
    public String getClassName(){
        return "Company";
    }
}
public class Demo1 {
    public static void main(String[] args){
        Company company=new Company();
        System.out.println("该类的类名称:"+company.getClassName());
    }
}

运行结果:

该类的类名称:Company

2、编写Graph类

题目:考虑一个表示图形的类,写出类中的属性及方法。

代码如下:

abstract class Graph{
    public abstract float area();
}
class Triangle extends Graph{
    private float length,wide;
    public Triangle(float length,float wide){
        this.setLength(length);
        this.setWide(wide);
    }
    public void setLength(float length){
        this.length=length;
    }
    public void setWide(float wide){
        this.wide=wide;
    }

    public float getLength() {
        return length;
    }
    public float getWide(){
        return wide;
    }
    public float area(){
        return getLength()*getWide()/2;
    }
}
public class Demo2 {
    public static void main(String[] args){
        Triangle triangle=new Triangle(30,23);
        System.out.println("面积为:"+triangle.area());
    }
}

运行结果:

面积为:345.0

3、建立一个人类(Person)和学生类(Student)

题目:功能要求如下:
(1) person中包含4个保护型的数据成员name、addr、sex、age,分别为字符串型、字符串型、字符型及整型,表示姓名、地址、性别和年龄。用一个4参构造方法、一个两参构造方法、一个无参构造方法、一个输出方法显示4种属性。
(2)Student类继承person类,并增加输出成员math、english存放数学和英语成绩。用一个6参构造方法、一个两参构造方法、一个无参构造方法和重写输出方法用于显示6 种属性。

代码如下:

class Person{
    protected String name,addr;
    protected char sex;
    protected int age;
    public Person(){}
    public Person(String name,char sex){
        this.name=name;
        this.sex=sex;
    }
    public Person(String name,String addr,char sex,int age){
        this.name=name;
        this.sex=sex;
        this.addr=addr;
        this.age=age;
    }
    public void print(){
        System.out.println("姓名:"+this.name+" ,性别:"+this.sex+" ,年龄:"+this.age+" ,地址:"+this.addr);
    }
}
class Student extends Person{
    private int math,english;
    public Student(){}
    public Student(String name,char sex){
        super(name,sex);
    }
    public Student(String name,String addr,char sex,int age,int math,int english){
        super(name,addr,sex,age);
        this.math=math;
        this.english=english;
    }
    public void print(){
        System.out.println("姓名:"+this.name+" ,性别:"+this.sex+" ,年龄:"+this.age+" ,地址:"+this.addr+" ,数学成绩:"+this.math+" ,英语成绩:"+this.english);
    }
}
public class Demo3 {
    public static void main(String[] args){
        Student stu1=new Student();
        Student stu2=new Student("张三",'男');
        Student stu3=new Student("李四","XXX",'男',20,98,88);
        stu1.print();
        stu2.print();
        stu3.print();
    }
}

运行结果:

姓名:null ,性别: ,年龄:0 ,地址:null ,数学成绩:0 ,英语成绩:0
姓名:张三 ,性别:男 ,年龄:0 ,地址:null ,数学成绩:0 ,英语成绩:0
姓名:李四 ,性别:男 ,年龄:20 ,地址:XXX ,数学成绩:98 ,英语成绩:88

4、定义员工类

题目:定义员工类,具有姓名、年龄、性别属性,并具有构造方法和显示数据方法。
定义管理层类,继承员工类,并有自己的属性职务和年薪。
定义职员类,继承员工类,并有自己的属性所属部门和月薪。

代码如下:

class Employees{
    private String name;
    private int age;
    private char sex;
    public Employees(){}
    public Employees(String name,int age,char sex){
        this.setName(name);
        this.setAge(age);
        this.setSex(sex);
    }
    public void setName(String name){
        this.name=name;
    }
    public void setAge(int age){
        this.age=age;
    }
    public void setSex(char sex){
        this.sex=sex;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
    public char getSex(){
        return sex;
    }
    public String toString(){
        return "name="+getName()+",age="+getAge()+",sex="+getSex();
    }
}
class Management extends Employees{
    private String job;
    private float yearSalary;
    public Management(String name,int age,char sex,String job,float yearSalary){
        super(name,age,sex);
        this.setJob(job);
        this.setYearSalary(yearSalary);
    }
    public void setJob(String job){
        this.job=job;
    }
    public void setYearSalary(float yearSalary){
        this.yearSalary=yearSalary;
    }
    public String getJob(){
        return job;
    }
    public float getYearSalary(){
        return yearSalary;
    }
    public String toString() {
        return "name="+getName()+",age="+getAge()+",sex="+getSex()+",job="+getJob()+",yearSalary="+getYearSalary();
    }
}
class Staff extends Employees{
    private String department;
    private float monthSalary;
    public Staff (String name,int age,char sex,String department,float monthSalary){
        super(name,age,sex);
        this.setDepartment(department);
        this.setMonthSalary(monthSalary);
    }
    public void setDepartment(String department){
        this.department=department;
    }
    public void setMonthSalary(float monthSalary){
        this.monthSalary=monthSalary;
    }
    public String getDepartment(){
        return department;
    }
    public float getMonthSalary(){
        return monthSalary;
    }
    public String toString() {
        return "name=" + getName() + ",age=" + getAge() + ",sex=" + getSex()+",department="+getDepartment()+",monthSalary="+getMonthSalary();
    }
}
public class Demo4 {
    public static void main(String[] args) {
        Management management=new Management("张三",40,'男',"经理",200000.0f);
        Staff staff=new Staff("李华",28,'男',"宣传",4000.0f);
        System.out.println(management.toString());
        System.out.println(staff.toString());
    }
}

运行结果:

name=张三,age=40,sex=男,job=经理,yearSalary=200000.0
name=李华,age=28,sex=男,department=宣传,monthSalary=4000.0

5、使用面向对象的概念表示出下面的生活场景

场景:小明去超市买东西,所有买到的东西都放在了购物车之中,最后到收银台一起结账。

分析
(1)购物车放置商品,有生活用品、食物、文具等;
(2)商品属性应该具有商品名、价格、数量、生产日期等;

(3)设置一个接口存储每一个商品类都可以用到的函数;
(4)将收银台的结账功能放置到购物车类中。

代码如下:

interface Goods
{
    public String getName();//取得产品名
    public String getProduction();//取得生产日期
    public double getPrice();//取得产品价格
    public int getNumber();//取得数量
    public void print();//打印信息
}
class Daily implements Goods
{
    private String production;
    private String name;
    private double price;
    private int number;
    public Daily(String production,String name,double price,int number)
    {
        this.production=production;
        this.name=name;
        this.price=price;
        this.number=number;
    }
    public String getName()//取得产品名
    {
        return this.name;
    }
    public String getProduction()//取得生产日期
    {
        return this.production;
    }
    public double getPrice()//取得产品价格
    {
        return this.price;
    }
    public int getNumber()
    {
        return this.number;
    }
    public void print()//打印信息
    {
        System.out.println("产品名:"+this.getName()+'\n'+"生产日期:"+this.getProduction()+'\n'+"价格:"+this.getPrice()+'\n'+"数量:"+this.getNumber()+'\n');
    }
}
class Food implements Goods
{
    private String production;
    private String name;
    private double price;
    private int number;
    public Food(String production,String name,double price,int number)
    {
        this.production=production;
        this.name=name;
        this.price=price;
        this.number=number;
    }
    public String getName()//取得产品名
    {
        return this.name;
    }
    public String getProduction()//取得生产日期
    {
        return this.production;
    }
    public double getPrice()//取得产品价格
    {
        return this.price;
    }
    public int getNumber()
    {
        return this.number;
    }
    public void print()//打印信息
    {
        System.out.println("产品名:"+this.getName()+'\n'+"生产日期:"+this.getProduction()+'\n'+"价格:"+this.getPrice()+'\n'+"数量:"+this.getNumber()+'\n');
    }
}
class Stationary implements Goods//定义文具类
{
    private String production;
    private String name;
    private double price;
    private int number;
    public Stationary(String production,String name,double price,int number)
    {
        this.production=production;
        this.name=name;
        this.price=price;
        this.number=number;
    }
    public String getName()//取得产品名
    {
        return this.name;
    }
    public String getProduction()//取得生产日期
    {
        return this.production;
    }
    public double getPrice()//取得产品价格
    {
        return this.price;
    }
    public int getNumber()
    {
        return this.number;
    }
    public void print()//打印信息
    {
        System.out.println("产品名:"+this.getName()+'\n'+"生产日期:"+this.getProduction()+'\n'+"价格:"+this.getPrice()+'\n'+"数量:"+this.getNumber()+'\n');
    }
}
class ShopCar
{
    private Goods[] goods;//使用接口接收对象
    private int foot;//数据的保存位置
    public ShopCar(int len)
    {
        if(len>0)
            this.goods=new Goods[len];
        else
            this.goods=new Goods[1];
    }
    public boolean add(Goods g)
    {
        if(this.foot<this.goods.length)//自己的对象可以直接调用私有数据成员
        {
            this.goods[foot]=g;
            this.foot++;
            return true;
        }
        else
            return false;
    }
    public void search(String keyword)
    {
        for(int i=0;i<this.goods.length;i++)
        {
            if(this.goods[i]==null)
                System.out.println("你没有买任何东西");
            if(this.goods[i].getName().equals(keyword))//不可以直接引用name
            {
                this.goods[i].print();
            }
        }
    }
    public double bill()
    {
        double sum = 0;
        for(int i=0;i<this.goods.length;i++)
        {
            if(this.goods[i]!=null)
            {
                sum+=this.goods[i].getPrice()*this.goods[i].getNumber();
            }
        }
        return sum;
    }
}
public class Demo5 {
    public static void main(String[] args) {
        ShopCar sc=new ShopCar(5);
        sc.add(new Food("火腿肠","2020.10.23",2.00,12));
        System.out.println(sc.bill());
    }
}

运行结果:

24.0


和我一起来刷题学习吧!传送门牛客网-求职|面试|学习

你可能感兴趣的:(java,开发语言,算法)