当前位置:   article > 正文

Java创建student类_java编写一个学生类student

java编写一个学生类student

目录

一.创建包,包名可以自拟,在包中创建student类

二.创建Penpal类继承student类

三.在HelloWorld中实现测试


1.1创建一个叫做Student的类:

对象属性:姓名、年龄、性别、身高

对象方法:说话、计算加法、年龄长一岁

类属性:学校名称

类方法:学习

1.2编写能为所有属性赋值的构造方法

封装所有对象属性,编写属性的getter和setter方法;

/创建包,包名可以自拟,在包中创建student类

  1. package Student;
  2. public class Student {
  3. //构造器
  4. public Student(String Name,String Sex,int Age,int height) {
  5. this.Name=Name;
  6. this.Sex=Sex;
  7. this.height=height;
  8. this.Age=Age;
  9. }
  10. public String Name; //学生姓名
  11. public int Age; //学生年龄
  12. public String Sex; //学生性别
  13. public int height;//学生身高
  14. public Student() {
  15. }
  16. //getter/setter方法
  17. public int getHeight() {
  18. return height;
  19. }
  20. public void setHeight(int height) {
  21. this.height = height;
  22. }
  23. public String getName() {
  24. return Name;
  25. }
  26. public void setName(String name) {
  27. this.Name = name;
  28. }
  29. //当方法的局部变量和类的成员变量重名的时候,根据就近原则,优先使用局部变量。
  30. //如果使用访问类的成员变量,使用this.关键字.
  31. //谁调用的方法谁就是this
  32. public void setAge(int age) {
  33. this.Age = age;
  34. }
  35. public int getAge() {
  36. return Age;
  37. }
  38. //类属性,学校名称
  39. public static String school="北京";
  40. //类方法,学习
  41. public static void study()
  42. {
  43. String study="开始学习了!";
  44. System.out.println(study);
  45. }
  46. //对象方法:说话
  47. public void speak(String s)
  48. {
  49. System.out.println(s);
  50. }
  51. //对象方法:实现两个数的相加
  52. public void count(double c1,double c2)
  53. {
  54. System.out.println(c1+c2);
  55. }
  56. //对象方法:实现年龄长一岁
  57. public int ageAdd(int year) {
  58. Age=year+1;
  59. return this.Age+1;
  60. }
  61. public void show()
  62. {
  63. System.out.println("我的姓名是"+Name+",今年"+Age+",性别:"+Sex+",身高:"+height);
  64. }
  65. }

2.1创建Penpal类继承student类

  1. package Student;
  2. public class Penpal extends Student{
  3. private String Sex;
  4. public String getSex(){
  5. return Sex;
  6. }
  7. public void setSex(String Sex){
  8. this.Sex=Sex;
  9. }
  10. }

3.1在HelloWorld中实现测试

  1. package Student;
  2. import Student.Penpal;
  3. import Student.Student;
  4. import static Student.Student.study;
  5. public class HelloWorld {
  6. public static void main(String[] args) {
  7. //类属性,学校名称
  8. System.out.println(Student.school);
  9. //类方法:学习
  10. study();
  11. //创建第一个实例:园子
  12. Student zhang=new Student();
  13. zhang.Age=9;
  14. zhang.height=175;
  15. zhang.Name="园子";
  16. zhang.Sex="男";
  17. System.out.println("姓名:"+zhang.Name+" 性别:"+zhang.Sex+"\t年龄:"+zhang.Age+"\t身高:"+zhang.height);
  18. //创建第二个实例:团后
  19. Student li=new Student();
  20. li.Name="团后";
  21. li.Age=15;
  22. li.height=198;
  23. li.Sex="女";
  24. System.out.println("姓名:"+li.Name+" 性别:"+zhang.Sex+"\t年龄:"+li.Age+"\t身高:"+li.height);
  25. //创建第三个实例
  26. Student p1=new Student("张老三","男",18,180);
  27. p1.speak("你好,我是新来的学生");
  28. //说话
  29. p1.show();
  30. //实现年龄的加一
  31. Student p2=new Student("王老五","女",10,158);
  32. p2.speak("你好,我是新来的学生");
  33. //实现年龄的加一
  34. p2.ageAdd(p2.Age);
  35. p2.show();
  36. //实现两个数的相加
  37. System.out.println("两个数的相加结果如下:");
  38. p2.count(23, 45);
  39. //实现对setter/getter的使用
  40. Penpal Student=new Penpal();
  41. Student.setSex("女");
  42. Student.setName("老纪");
  43. Student.setAge(12);
  44. Student.setHeight(156);
  45. System.out.println("姓名:"+Student.getName()+" "+"性别:"+Student.getSex()+" "+"年龄:"+Student.getAge()+" "+"身高:"+Student.getHeight()+"\n");
  46. }
  47. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/244496
推荐阅读
相关标签
  

闽ICP备14008679号