Chinaunix首页 | 论坛 | 博客
  • 博客访问: 358660
  • 博文数量: 81
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 847
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-25 22:29
个人简介

执一不失,能君万物http://weidian.com/s/284520723?wfr=c

文章分类

全部博文(81)

文章存档

2016年(11)

2015年(70)

我的朋友

分类: Java

2015-07-16 21:17:10

将对象作为参数传递给方法
public  Student(String name,int a,String m){
    stuName = name;
    age = a;
    major = m;
}
声明对象的时候
Student stu = new Student("Jerry",22,"Java");

点击(此处)折叠或打开

  1. package com.troubleshooting.java;


  2. public class Student {

  3.     private String stuName;
  4.     private int age;
  5.     private String major;

  6.     public String getStuName() {
  7.         return stuName;
  8.     }

  9.     public void setStuName(String stuName) {
  10.         this.stuName = stuName;
  11.     }

  12.     public int getAge() {
  13.         return age;
  14.     }

  15.     public void setAge(int age) {
  16.         this.age = age;
  17.     }

  18.     public String getMajor() {
  19.         return major;
  20.     }

  21.     public void setMajor(String major) {
  22.         this.major = major;
  23.     }

  24.     public Student(String n, int a, String m) {
  25.         stuName = n;
  26.         age = a;
  27.         major = m;
  28.     }

  29. }

  30. package com.troubleshooting.java;

  31. public class TestStudent {
  32.     public static void main(String[] args) {
  33.         Student stu = new Student("Jerry", 22, "Java");
  34.         // Student stu = new Student();
  35.         // stu.setStuName("Tom");
  36.         // stu.setMajor();
  37.         // stu.setAge();

  38.         System.out.println(stu.getStuName());
  39.         System.out.println(stu.getAge());
  40.         System.out.println(stu.getMajor());

  41.     }
  42. }

阅读(1752) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~