5.7作业

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

package itheima01.com;
public class student {
    String name = new String("该学生尚未命名");
    double score;
 
    public student() {
    }
 
    public student(String str, double score) {
        this.name = str;
        this.score = score
    }
 
    void set(String str, double score) {
        this.name = str;
        this.score = score;
    }
 
    void get() {
        System.out.println("该学生姓名为:" + name);
        System.out.println("该学生成绩为:" + score);
    }
}

 
    package itheima01.com;



    
public class HelloWorld {
    
            
    public static void main(String args[]) {
        student A = new student();
        A.set("xiao gou", 100);
        A.get();
        student B = new student("wanger", 20);
        B.get();
    }
}


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

package itheima01.com;
 


    public class Person {
        public void Test() {
            System.out.println("无参的调用方法被调用了");
        }
    }
package itheima01.com;

public class Text {
    public static void main(String[] args) {

        Person z=new Person();
        z.Test();
    }

}



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

package itheima01.com;

public class Car {
    
         
         String name; 

         String color;
         
        
         public void  run(){
          System.out.println(name+"是一款顶级豪车.");
         }
        }
package itheima01.com;



    
public class HelloWorld {
public static void main(String[] args) {
    
            
    Car c = new Car(); 
      
      
      c.name = "路虎";
      c.color = "黑色";
      
      System.out.println("名字:"+ c.name+" 颜色:"+ c.color);
      
      c.run();  
     }
    }



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

package itheima01.com;

public class Demo {

        public static void main(String[] args) {
                    Demo z = new Demo();
                    int k = z.get(11, 55);
                    System.out.println(k);
                }
                public int get(int i,int j) {
                    return i+j;
                }
            }


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

封装定义:是指隐藏对象的属性和实现细节,仅对外提供公共访问方式。
好处:1.隐藏实现细节,提供公共访问方式;
           2.提高了代码的复用性;
           3.提高安全性;
set访问器是给属性赋值的,get访问器是取得属性值的
原文地址:https://www.cnblogs.com/Syz1107/p/12842260.html