• java第11次作业


    5、定义两个类,描述如下: [必做题]
    • 5.1定义一个人类Person:
    • 5.1.1定义一个方法sayHello(),可以向对方发出问
    候语“hello,my name is XXX”
    • 5.1.2有三个属性:名字、身高、体重
    • 5.2定义一个PersonCreate类:
    • 5.2.1创建两个对象,分别是zhangsan,33岁,1.73
    ;lishi,44,1.74
    • 5.2.2分别调用对象的sayHello()方法。

    public class Person {
        String name;
        double height;
        int age;
        Person(String name,double height, int age) {
            this.name = name;
            this.height = height;
            this.age = age;
        }
        void sayHello() {
            System.out.println("hello,my name is  "+name +"  "+height+" m "+" "+age+ "岁");
        }
    }
    public class PersonCreate {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Person a = new Person("zhangsan", 1.73,33);
            Person b = new Person("lishi",1.74,44);
            a.sayHello();
            b.sayHello();
    
        }
    
    }

    3、定义一个笔记本类,该类有颜色(char)和cpu
    型号(int)两个属性。 [必做题]
    • 3.1 无参和有参的两个构造方法;有参构造方法可
    以在创建对象的同时为每个属性赋值;
    • 3.2 输出笔记本信息的方法
    • 3.3 然后编写一个测试类,测试笔记本类的各个

    public class Computer {
        
         private char color;
         private int cpu;
         public Computer(){
             
         }
         public Computer(char color,int cpu){
             this.color=color;
             this.cpu=cpu;
         }
         public char getColor(){
             return color;
         }
         public void setColor(char color){
             this.color=color;
         }
         public int getCpu(){
             return cpu;
         }
         public void setCpu(int cpu){
             this.cpu=cpu;
         }
         public void showComputer(){
             System.out.println("笔记本的颜色是: "+getColor());
             System.out.println("cpu型号:"+getCpu());
             
         }
     }
    public class TestComputer {
    
        public static void main(String[] args) {
             Computer a = new Computer();
             a.showComputer();
             Computer b = new Computer('黑',66668888);
             b.showComputer();
    
        }


     

  • 相关阅读:
    揭秘重度MMORPG手游后台性能优化方案
    算法:贪心、回溯(su)、分治、动态规划,思想简要
    表单提交 curl和浏览器方式
    mysql 聚集索引,非聚集索引,覆盖索引区别。
    虚拟机中的Linux不能上网
    第二篇 界面开发 (Android学习笔记)
    第一篇 入门必备 (Android学习笔记)
    C/C++知识补充 (1)
    向后/向前引用, 零宽断言, 转义, 命名分组
    C/C++知识补充(2) C/C++操作符/运算符的优先级 & 结合性
  • 原文地址:https://www.cnblogs.com/zjzj123/p/12759658.html
Copyright © 2020-2023  润新知