按要求编写java应用程序:
- 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();
- }
- }
编写java应用程序:
- 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应用程序:
- 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();
- }
- }
编造一个程序,计算箱子的体积,将每个箱子的高度、宽度和长度参数的值传递给构造方法,计算并显示体积.
- 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语法的相关练习.......
来源: http://www.cnblogs.com/yjlblog/p/7594288.html