参数的一些常见问题

/*

  给变量赋值时,参数就是我们常用的方式之一

*/

/*

    一. 成员方法带参数的

*/

public class Student {
   String name;
   int age;
   public void addName(String name,int age){
      this.name=name;
      this.age=age;
   }
   public void show(){
      System.out.print(this.name+"的年龄是:"+this.age);
   }
}

 

public class TestStudent {
   public static void main(String[] args){  
      Student s=new Student();
      s.addName("上官婉儿",18); //调用方法传参
      s.show();
   }
}

/*  二   setXxx(){} 方法,   getXxx(){}方法

*/

public class Student2 {
   String name;
   int age;
   public String getName(){
      return name;
   }
   public void setName(String name){
      this.name=name;
   }
   public int getAge(){
      return age;
   }
   public void setAge(int age){
      this.age=age;
   }
   public void show(){
      System.out.println(this.getName()+"的年龄是:"+this.getAge());
   }
}

 

public class TestStudent2 {
   public static void main(String[] args){
      Student2 s=new Student2();
      s.setName("百里登峰");  //调用方法传参
    s.setAge(18);
      s.show();
   }
}

/* 三    带参构造函数

*/
public class Student1 {
   String name;
   int age;
   public Student1(String name,int age){
      this.name=name;
      this.age=age;
   }
   public void show(){
      System.out.print(name+"的年龄是:"+age);
   }
}

 

public class TestStudent1 {
   public static void main(String[] args){
      Student1 s=new Student1("西门吹雪",20);  //直接传参
      s.show();
   }
}

 

posted @ 2017-04-07 21:56  末法时代的谱写者  阅读(168)  评论(0编辑  收藏  举报