Java笔记—— 类与对象的几个例子

时间:2023-03-10 06:01:36
Java笔记—— 类与对象的几个例子

问题1

按要求编写java应用程序:

  • 编写西游记人物类,属性有:身高,名字和武器。方法有:显示名字,显示武器。
  • 在main方法中创建两个对象。猪八戒和孙悟空,并分别为他们的两个属性名字和武器赋值,最后分别调用显示名字和显示武器的方法,显示两个对象的属性值。

代码实现

package test;

public class monky
{
    double hight;
    String name;
    String weapon;
    void printName()
    {
        System.out.println("The name is :"+name);
    }
    void printWeapon()
    {
        System.out.println("The weapon is:"+weapon);
    }
    public static void main(String[] args)
    {
        monky monkey = new monky();//因为没有声明构造方法,所以系统默认生成一个没有参数的构造方法
        monky pig = new monky();
        monkey.name = "Sun Wukong";
        monkey.weapon = "Golden cudgel";
        pig.name = "Pigzy";
        pig.weapon = "Nine tooth a";
        monkey.printName();
        monkey.printWeapon();
        pig.printName();
        pig.printWeapon();

    }
}

问题2

编写java应用程序:

  • 定义一个学生类(Student),包括学号、姓名和年龄属性。两个方法:setStudent用于对对象的初始化,output用于输出学生信息。
  • 再定义一个类TestClass,在main方法中创建多个Student对象,使用这些对象测试Student类的功能。

    代码实现

package test;

public class Student
{
    String studentNumber;
    String studuentName;
    int studentAge;

    void setStudent(String studentNumber,String studentName,int studentAge)
    {
        this.studentNumber = studentNumber;//对于成员变量和参数相同的情况,
                                       //用 this 作为关键字
        this.studuentName = studentName;
        this.studentAge = studentAge;

    }
    void output()
    {
        System.out.println("The students' information is");
        System.out.println("Student's Name:"+studuentName);
        System.out.println("Student's NO.:"+studentNumber);
        System.out.println("Student's Age:"+studentAge);
    }
    public static void main(String[] args)
    {
        Student s1 = new Student();
        s1.setStudent("134982394", "June", 18);
        s1.output();
    }

}

小结

  • 对于Java中的构造方法和C语言中的结构体很类似,也是自己定义的一种新的数据类型
    对于public 本来的意思就是“公共”的意思,也就是说我们自己定义的这个public Student 这个
    类也可以给别人进行使用,只要使用者 新建一个类,然后声明 自己 要调用的"构造的方法"就可以使用。
  • 而在java中的“方法”和C语言中的函数也差不多

问题3

编写java应用程序:

  • 定义一个学生类(Student),包括学号、姓名和年龄属性。在构造方法中对对象的初始化。
  • output用于输出学生信息。

代码实现

package www.yjlblog.cn;

public class Student
{
    String stdNum;
    String stdName;
    int stdAge;
    Student(String stdNum,String stdName,int stdAge)
    {
        this.stdName = stdName;
        this.stdNum = stdNum;
        this.stdAge = stdAge;

    }
     void output()
    {
        System.out.println("Student's Name:"+stdName);
        System.out.println("Student's Number:"+stdNum);
        System.out.println("Student's Age:"+stdAge);
    }
    public static void main(String[] args)
    {
        Student s1 = new Student("16023873","June",18);
        //对于“构造方法”中,如果你构造的方法里有参数,那么在使用的时候也是要带参数的
        s1.output();

    }
}

问题4

编造一个程序,计算箱子的体积,将每个箱子的高度、宽度和长度参数的值传递给构造方法,计算并显示体积.

代码实现

package www.yjlblog.cn;

/**
 * question:编造一个程序,计算箱子的体积,将每个箱子的高度、宽度和长度参数
 *          的值传递给构造方法,计算并显示体积
 * author:yjl
 * time:2017/9/25*/
public class box
{
    double lenth;
    double width;
    double hight; 

    box(double lenth,double width,double hight)
    {
        this.lenth = lenth;
        this.width = width;
        this.hight = hight;

    }
    double volume()//因为在类 box 中已经定义了“全局”(相对于这类box来说)成员变量,所以此方法里不含有参数
    {
        double V = lenth*width*hight;
        return V;
    }

    void output()
    {
        System.out.println("The box's volume is:"+volume());
    }
    public static void main(String[] args)
    {
        box n1 = new box(3,4,6);

        n1.output();

    }
}

总结

这几个小例子,主要是进行java语法的相关练习.......