将对象作为参数传递给方法
public Student(String name,int a,String m){
stuName = name;
age = a;
major = m;
}
声明对象的时候
Student stu = new Student("Jerry",22,"Java");
-
package com.troubleshooting.java;
-
-
-
public class Student {
-
-
private String stuName;
-
private int age;
-
private String major;
-
-
public String getStuName() {
-
return stuName;
-
}
-
-
public void setStuName(String stuName) {
-
this.stuName = stuName;
-
}
-
-
public int getAge() {
-
return age;
-
}
-
-
public void setAge(int age) {
-
this.age = age;
-
}
-
-
public String getMajor() {
-
return major;
-
}
-
-
public void setMajor(String major) {
-
this.major = major;
-
}
-
-
public Student(String n, int a, String m) {
-
stuName = n;
-
age = a;
-
major = m;
-
}
-
-
}
-
-
package com.troubleshooting.java;
-
-
public class TestStudent {
-
public static void main(String[] args) {
-
Student stu = new Student("Jerry", 22, "Java");
-
// Student stu = new Student();
-
// stu.setStuName("Tom");
-
// stu.setMajor();
-
// stu.setAge();
-
-
System.out.println(stu.getStuName());
-
System.out.println(stu.getAge());
-
System.out.println(stu.getMajor());
-
-
}
-
}
阅读(1752) | 评论(0) | 转发(0) |