日期:2014-05-20 浏览次数:20822 次
package csdn; public class StudentTest { public static void main(String args[]) { StudentV5 s1 = new StudentV5("laoda", "M", 21); System.out.println(s1); // 在没有重写toString的情况下,这个打印的是 该对象在内存中的唯一标识,由对象名+散列码组成的 } } class StudentV5 { private String Name; // 变量开头字母为小写 如:name private String Sex; private int Grade; public StudentV5(String name, String sex, int grade) { System.out.println("初始化!————"); Setname(name); // 初始化不必这样 直接 this.name = name Setgrade(grade); Setsex(sex); } public void Setname(String name) { Name = name; } public void Setgrade(int grade) { Grade = grade; } public void Setsex(String sex) { Sex = sex; } }
------解决方案--------------------
sorry啊,我2楼的那个是错的,没编译就提上来了
package com.snbc.study; public class Student { private int grade; private String name; private String gender; public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public void addStudent(int grade, String name,String gender){ setName(name); setGender(gender); setGrade(grade); } }
------解决方案--------------------