/**
 * 学生类
 * ctrl+o  查询本类的所有属性 和方法
 */
public class Student {
    int age; // 年龄
    String name; // 姓名

    // 自己创建一个本类的 无参 构造方法
    public Student() {
    }

    /**
     *  创建一个本类的 带参 构造方法
     * @param name   用户传递过来的  姓名
     * @param age    用户传递过来的  年龄
     * 
     * name 和age  都是我们Student带参构造中的 形参 ! 也是 局部变量!
     *  形参和 成员变量 同名了???
     *  取谁?实际用的是哪个变量?
     *  怎么解决这个同名的问题呢??
     *  使用关键字 this ==>当前类的对象
     */
    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }
public class StudentTest {

    public static void main(String[] args) {
        /**
         *  通过Student类来创建我们一个学生对象
         *  Student() 无参构造方法
         */
        Student stu = new Student();
        stu.name = "aa";
        stu.age = 20;
        System.out.println(stu.name); // 出生的娃娃没有起名
        System.out.println(stu.age);

        System.out.println("------------------------------------------");
        // 再次创建一个学生对象 调用 带参构造方法
        Student stu2 = new Student(25, "小黑");
        System.out.println(stu2.name); // 出生的娃娃有名字
        System.out.println(stu2.age);
    }

}
/**
 * 学生类
 */
public class Student {
    String name; // 姓名

    /**
     * 带参构造
     */
    public Student(String name) {
        this.name = name;
    }

    /**
     *  购物的方法
     *  01.没有返回值
     *  02.没有参数
     */
    public void buy() {
        System.out.println(name + "在购物");
    }

    /**
     * @param money  某人消费的金额
     * double money  形参
     */
    public void buy(double money) {
        System.out.println(name + "在购物,消费金额:" + money);
    }

    /**
     * @param money 某人消费的金额
     * @param name  某人的姓名
     * @return   买了什么东西
     * 
     * return  跳出方法体 并且带有返回值!
     * 返回值的类型 必须是 方法的返回值类型!
     * 
     */
    public String buy(double money, String name) {
        String result = name + "在购物,消费金额:" + money + "==>得到一大包东西";
        return result;
    }

}
public class StudentTest {

    public static void main(String[] args) {

        // 创建一个学生对象
        Student stu = new Student("小红");
        String result = stu.buy(500, "小白");
        System.out.println(result);

    }

}

 

posted on 2017-08-18 12:48  我可不是隔壁的老王  阅读(142)  评论(0编辑  收藏  举报