zoukankan      html  css  js  c++  java
  • java第十周上机练习

    1、请按照以下要求设计一个学生类Student,并进行测试。
    要求如下:
    1)Student类中包含姓名、成绩两个属性
    2)分别给这两个属性定义两个方法,一个方法用于设置值,另一个方法用于获取值.
    3)Student类中定义一个无参的构造方法和一个接收两个参数的构造方法,两个参数分别为姓名和成绩属性赋值
    4)在测试类中创建两个Student对象,一个使用无参的构造方法,然后调用方法给姓名和成绩赋值,一个使用有参的构 造方法,在构造方法中给姓名和成绩赋值

    public class student {
    	String name = new String("该学生没有命名");
        double result;
        public student(){
        }
        public student(String str,double result){
            this.name = str;
            this.result = result;
            }
        void set(String str,double result){
            this.name = str;
            this.result = result;
            }
        void get(){
            System.out.println("该学生的姓名:"+name);
            System.out.println("该学生的成绩:"+result);
            }
        } 

      

    public class test {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		  student a = new student();
    	      a.set("seven", 99);
    	      a.get();
    	      student b = new student("allen",98);
    	      b.get();
    	}
    
    }
    

    2、请编写一个程序,该程序由两个类组成,一个Person类,一个Test类。在Person类中定义一个无参构造方法,里面 输出一句话:”无参的构造方法被调用了...”。并在测试类中进行测试。

    package xixi;
    
    public class Person {
    	public Person(){
            System.out.println("无参的构造方法被调用了······");
        }
    }
    

      

    package xixi;
    
    public class test {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		 Person b = new Person();
    	}
    
    }
    

      

    3. 使用java类描述一个车类,车都具备名字、颜色两个属性,还具备跑的功能。 请设计一个汽车类Car,该类中包含 两个属性姓名(name)、颜色(color),一个用于描述汽车跑的run()方法。

    public class cartype {
    	String name = new String("车的品牌:");
        String color = new String("车的颜色:");
        public cartype(String name,String color){
            this.name = name;
            this.color = color;
            run();
        }
        public void run(){
            System.out.println(color+"的"+name+"已经驶出!");
        }
    }
    

      

    public class text {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new cartype("奥迪","黑色");
    	    new cartype("宝马","白色");
    	}
    
    }
    

      


    4. 编写一个类,类中定义一个静态方法,用于求两个整数的和。 请按照以下要求设计一个测试类Demo,并进行测试。  要求如下: 
      1)Demo类中有一个静态方法get(int a,int b)该方法用户返回参数a、b两个整数的和; 
      2)在main()方法中调用get方法并输出计算结果。

    public class demo {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		 int a = 411;
    	     int b = 531;
    	       System.out.println(get(a,b));
    	    }
    	    private static int get(int a,int b){
    	        return a+b;
    	}
    
    }
    

      


    5.说一下什么是封装, 使用封装的好处。什么是get,set访问器

    get访问器相当于一个无参数方法,该方法具有属性类型的返回值以及属性相同的修饰符,而执行get访问器就是相当于读取了字段的值。需要注意的是,在get访问器中,返回值作为属性值提供给调用表达式。
    set访问器相当于一个void方法,该方法具有单个属性类型的值参数,以及包含属性相同的修饰符。
    在set访问器中,新的属性值通过set访问器的参数传递。值得注意的是,在通过新的属性值赋值给字段时,显示参数传递必须声明为与该属性相同的数据类型,当然也可以使用隐式参数,不加声明时,编译器将使用隐式参数Value来表示赋给属性的新值。set访问器隐式具有名为value的参数(value),此参数的类型是属性的类型。

  • 相关阅读:
    Spring Boot学习笔记
    打造高效率的软件测试
    如何将测试结果在jenkins上发布
    如何在docker container中运行web自动化测试
    Jmeter中随机读取测试文件的内容
    如何提高UI自动化测试的质量
    mac系统上添加定时任务
    keypass口令管理实践
    GPG实践
    树的遍历
  • 原文地址:https://www.cnblogs.com/qq007/p/12842206.html
Copyright © 2011-2022 走看看