栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Java面向对象练习题

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java面向对象练习题

练习题

题目分析

总结

题目分析

一.

定义一个Hero类
属性有 power,name,分别代表体力值和英雄的名子,体力值默认为100;
方法有
1.void go(); //行走的方法,如果体力值为0,则输出不能行走,此英雄已死亡的信息
2.void eat(int n); //吃的方法,参数是补充的血量,将 n的值加到属性power中,power的值最大为100,
3.void hurt();//每受到一次伤害,体力值-10,体力值最小不能小于0
编写测试类,来测试以上代码

思路:

1.读题,理解题目根据题目的给出的信息写代码

2.定义一个hero类,他的属性有体力和名字,体力值赋值为100。

3.定义三个方法,go方法if判断体力值是否大于0,大于0继续,小于等于0则输出已死亡。

eat(int n)方法中power加上传入的n,重新赋值power。   hurt方法中输入伤害次数,重新计算体力值,调用go方法,输出判断是否死亡。

代码

hero类:

package com.company.day15;

import java.util.Scanner;

public class Excel2 {
    double power=100;//体力值
    String name;
//    int n;//补充的血量

    //行走方法
    public void go(double power){
        if(power>0){
            System.out.println("继续游戏");

        }
            else if(power<=0){
            System.out.println("不能行走,英雄已经死亡");
        }

        }
        //参数是补充的血量,将 n的值加到属性power中,power的值最大为100,
    public double eat(int n){
        power=power+n;
        if(power>100){
            return 0;
        }
        return power;
    }
    //伤害方法
    // 每受到一次伤害,体力值-10,体力值最小不能小于0
    public double hurt(){
        System.out.println("输入伤害次数");
        Scanner sca=new Scanner(System.in);
        //伤害次数
        int count=sca.nextInt();
        power=power-count*10;
        if(power<=0){
         go(power);
        }
        return power;


    }
    public Excel2(double power,String name){
        this.power=power;
        this.name=name;
    }
}

测试:

package com.company.day15;
public class Test2 {
    public static void main(String[] args) {
        Excel2 excel2=new Excel2(20,"奥特曼");
//        System.out.println(excel2.power);//输出体力值
        //调用eat方法
//        double result=excel2.eat(-20);
//        System.out.println(result);

        //调用hurt方法
        double result2=excel2.hurt();
        System.out.println(result2);
    }
}

二.编写自定义类实现图书信息设置

任务:

属性:书名、作者、出版社、价格

方法:信息介绍

要求:

设计构造函数实现对属性赋值

设置私有属性,get/set方法实现对属性的访问

限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10

限定作者、书名均为只读属性

信息介绍方法描述图书所有信息

思路:

1.一样,先读题,理解题目。根据题目给出的信息写代码。

2.属性全部设置为private,生成get/set方法。

3.if判断图书价格,get获取价格,小于10的话用set方法将价格用改为10.

4.只读属性,是只可以读取不可以修改。把作者,书名的set方法删除。避免用set方法修改数值。

5.定义一个show方法,传入所有的属性,然后输出。展示所有的图书信息。

代码:

类:

package com.company.day15;

public class Excel3 {
    //属性
    private String name="平凡的世界";
    private String author="路遥";
    private String publishe;
    private double price;

    //方法
    //构造方法
    //限定作者、书名均为只读属性
    public Excel3(  String name,String author,String publishe, double price) {
        this.name = name;
        this.author = author;
        this.publishe = publishe;
        this.price = price;
    }

    //get/set方法实现对属性的访问
    public String getName() {
        return name;
    }

    public String getAuthor() {
        return author;
    }

    public String getPublishe() {
        return publishe;
    }
    public void setPublishe(String publishe) {
        this.publishe = publishe;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }

    //限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10
    //限定作者、书名均为只读属性
    //信息介绍方法描述图书所有信息
public void show(){
    System.out.println(getAuthor());
    System.out.println(getPrice());
    System.out.println(getName());
    System.out.println(getPublishe());
}

}

测试:

package com.company.day15;

public class Test3 {
    public static void main(String[] args) {
        //构造方法,创建对象,赋值
        Excel3 excel3=new Excel3("三体","刘慈欣","湖南出版社",0);

        //图书价格必须大于10,如果无效需进行提示,并强制赋值为10
        if(excel3.getPrice()<10){
            System.out.println("图书价钱较低,增为10元");
            excel3.setPrice(10);
        }
        //显示所有的信息
        excel3.show();
    }
}

三.编写程序描述IT从业者

要求:

1、设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问

2、限定IT从业人员必须年满15岁,无效信息需提示,并设置默认年龄为15。

3、工作方法通过输入参数,接收工作单位和职务,输出个人工作信息

4、编写测试类,测试IT从业者类的对象及相关方法(测试数据信息自定义)

思路:

1.这个题目之前我思考的有误,首先前面都是把属性私有化,通过set和get方法实现属性的访问。到了第三步我是在在类中写一个方法从控制台输入信息,把方法的值返回给属性,之后构造方法接收属性的值。最后调用构造方法创建对象,控制台输入值。

代码:
package com.company.day15;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.Scanner;
public class Excel4 {
    private int age=15;
    private String method;
    private String methodinforma=method1();//把方法返回的值赋值给属性informa

    public String getMethodinforma() {
        return methodinforma;
    }

    public void setMethodinforma(String methodinforma) {
        this.methodinforma = methodinforma;
    }

    //方法对属性的访问权限
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getMethod() {
        return method;
    }
    public void setMethod(String method) {
        this.method = method;
    }
    //3、工作方法通过输入参数,接收工作单位和职务,输出个人工作信息
    public String method1(){
        Scanner sca=new Scanner(System.in);
        String danwei=sca.next();
        String zhiwu=sca.next();
        String informa=danwei+" "+zhiwu;
        return informa;
    }
    public Excel4(String methodinforma,int age){//把属性的值传到构造方法
        this.methodinforma=methodinforma;
        this.age=age;
    }




}

结果:输出来的值,只是构造方法传入的值,控制台中输入的值则没有被打印

原因:通过构造方法创建对象,并且赋值,而我调用方法控制台输入值,算是给属性赋默认值。构造方法的赋值有掩盖默认值(自己理解的,如有错误,请指正,感谢)

优化

把方法去掉,控制台输入放到main方法中,之后用set方法修改构造方法赋的值。

类:

package com.company.day15;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.Scanner;
public class Excel5 {
    private int age=15;
    private String methodinforma;

    public String getMethodinforma() {
        return methodinforma;
    }

    public void setMethodinforma(String methodinforma) {
        this.methodinforma = methodinforma;
    }

    //方法对属性的访问权限
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Excel5(String methodinforma,int age){
        this.methodinforma=methodinforma;
        this.age=age;
    }

}

测试:
package com.company.day15;

import java.util.Scanner;

public class Test5 {
    public static void main(String[] args) {
        Excel4 excel4=new Excel4("aa",20);
        //3、工作方法通过输入参数,接收工作单位和职务,输出个人工作信息
        Scanner sca=new Scanner(System.in);
        System.out.println("单位");
        String danwei=sca.next();
        System.out.println("职务");
        String zhiwu=sca.next();
        String informa=danwei+" "+zhiwu;
        if(excel4.getAge()<=15){
            excel4.setAge(15);
        }
        excel4.setMethodinforma(informa);
        System.out.println(excel4.getMethodinforma());
        System.out.println(excel4.getAge());

    }
}

总结:

Java面向对象中给属性赋值的三种方法:

1.构造方法赋值。

2.set方法。

3.直接给属性赋初始值。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1018042.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号