java 方法的定义 类的定义

student.java

package com.company;

import javax.swing.plaf.synth.SynthOptionPaneUI;

/**
 * Created by dllo on 17/5/4.
 */
//定义一个类
//权限 关键字  类名
public class Student {
    //特征 - 变量
    private String name;
    private int age;
    private double score;
    //构造方法:
    //不写返回值类型, 名字必须与类名相同
    public Student(String name, int age, double score){
        this.name = name;
        this.age = age;
        this.score = score;
    }
    //当自己定义了构造方法,系统就不会再去创建
    //此时防止使用者调用不带参的构造方法报错,我们需要自定义一个
    public Student(){

    }
    //方法重载:
    //方法名相同,但是参数列表不同,被称为方法重载(overload)
    //参数列表不同代表:参数的类型,个数, 顺序不同
    //注意:重载与返回值类型无关

    public void setInfo(String name, int age, double score){
        this.name = name;
        this.age = age;
        this.score = score;
    }
    public void setName(String name){
        //this - 谁去调用setName方法,this就是谁
        this.name = name;
    }
    public String getName(){

        return name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public int getAge(){
        return age;
    }

    public void setScore(double score){
        this.score = score;
    }

    public double getScore(){
        return score;
    }



    //行为 - 方法(函数)
    public void info() {
        System.out.println(name);
        System.out.println(age);
        System.out.println(score);

    }
    //方法:
    //权限 返回值类型 方法名(参数1,参数2.....){
    //       }
    //形式1:无参无返回
    public void fun1(){
        int sum = 0;
        for (int i = 0; i < 3; i++){
            sum = sum + i;
        }
            System.out.println(sum);

    }
    //形式2:无参有返回值 - 好处是:可以将结果返回给调用者,让其决定处理方式
    public int fun2(){
        int sum = 0;
        for (int i = 0; i < 3; i++){
            sum = sum + i;
        }
//        System.out.println(sum);
        //return 要返回的具体数值
        // 注意:返回值的数值要与方法名前面的返回值类型一致
        return sum;
    }


    //形式3:有参有无返回值 - 好处是:灵活,兼容 可以让调用者决定计算范围
    public void fun3(int startNum, int endNum){
        int sum = 0;
        for (int i = startNum; i < endNum; i++){
            sum = sum + i;
        }
        System.out.println(sum);
    }

    //形式4:有参有返回值 - 好处是:兼顾了形式1和形式3的优势
    public int fun4(int startNum, int endNum){
        int sum = 0;
        for (int i = startNum; i < endNum; i++){
            sum = sum + i;
        }
//        System.out.println(sum);
        return sum;
    }

    //第一时期:先不写方法,将功能正常写在main函数内,然后再去看那些代码可以是共同完成一个功能,
    //       再创建无参数无返回值的函数,将代码剪切进去
    //第二时期:当第一时期很熟悉了以后,可以尝试先去创建无参数无返回值方法,
    //       并在其中编写功能代码,当需要某些变量信息却没有的时候,再去添加参数
    //第三时期:设计函数的时候考虑兼容性,考虑计算的结果放在方法内去操作好,
    //       还是返回给调用者操作好,再去决定使用什么形式的方法

    //练习 编写方法实现:
    //1.两个数的和,差
    public int  totalOfTwo(int a, int b){
        int total = a + b;
       return total;
    }
    public int  PoorOfTwo(int a, int b){
        int total = a + b;
        return total;
    }
    //2.两个数的最大值
    public int maxOfTwo(int a, int b){
        int max = 0;
        max = a > b ? a : b;
        return max;
    }

    //3.三个数的最大值
    public  int maxOfThree(int a , int b , int c ){
        int max = 0;
//        max = a > b ? a : b;
//        max = max > c ? max : c;
        max = maxOfTwo(a, b);
        max = maxOfTwo(max, c);
        return max;
    }


    //4.四个数的最大值
    public  int maxOfFoure(int a, int b , int c, int d){

        int max = 0;
//        max = a > b ? a : b;
//        max = max > c ? max : c;
//        max = max > d ? max : d;
        max = maxOfThree(a, b, c);
        max = maxOfTwo(max, d);

        return max;
    }

    //5.五个数的最大值
    public  int maxOfFive(int a, int b , int c, int d, int e){
        int max = 0;
//        max = a > b ? a : b;
//        max = max > c ? max : c;
//        max = max > d ? max : d;
//        max = max > e ? max : e;

        max = maxOfFoure(a, b, c, d);
        max = maxOfTwo(max, e);





        return max;
    }



}

Person.java

package com.company;

/**
 * Created by dllo on 17/5/4.
 */
public class Person {

   //1.私有的属性(变量)
    private String name;
    private int age;
    private String sex;
    private String constellation;

    //2,带参和不带参的构造方法
    public Person(String name, int age, String sex, String constellation){
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.constellation = constellation;
    }

    public Person(){

    }

    //3.为每一个变量写一对setter和getter方法
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }

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

    public int getAge(){
        return age;
    }

    public void setSex(String sex){
        this.sex = sex;
    }

    public String getSex(){
        return sex;
    }

    public void setConstellation(String constellation){
        this.constellation = constellation;
    }

    public String getConstellation(){
        return constellation;
    }

    //4,(可选)添加一个打印所有信息的方法
    public void info() {
        System.out.println(name);
        System.out.println(age);
        System.out.println(sex);
        System.out.println(constellation);
    }

}

Main.java 方法的调用

package com.company;

public class Main {

    public static void main(String[] args) {
//方法内部定义的放在栈区
        //定义对象
        //类名 对象名 = 内存中开辟空间  构造方法
//        Student stu = new Student();
        //使用对象 - 访问对象中的属性
//        stu.name = "貂蝉";
//        stu.age = 10;
//        stu.score = 80;
//        stu.info();

//        Student stu1 = new Student();
//        stu1.name = "妲己";
//        stu1.age = 364;
//        stu1.score = 150;
//        stu1.info();
//        stu.info();//重复输出不会覆盖已有的信息  而是开辟新的空间 已有的信息依旧会输出


//        Person per = new Person();
//        per.name = "妲己";
//        per.age = 1038;
//        per.sex = "女";
//        per.constellation = "白羊座";
//
//        per.info();

        //     Student stu = new Student();
//        stu.fun1();
//        int sum = stu.fun2();
//        System.out.println("可以任意处理fun2返回结果: " + sum);

//        stu.fun3(5, 10);

//        int sum = stu.fun4(3, 9);
//       System.out.println("总数 :" + sum);

//        int total = 0;
//        int poor = 0;
//        total = stu.totalOfTwo(3, 4);
//        poor = stu.PoorOfTwo(5, 3);
//        System.out.println("和:" + total);
//        System.out.println("差:" + poor);
//
//        int max = stu.maxOfTwo(7, 10);
//        System.out.println("最大值:" + max);
//
//        int max1 = stu.maxOfThree(6, 7, 9);
//        System.out.println("三个数最大值: " + max1);
//
//        int max2 = stu.maxOfFoure(7, 22, 44, 10);
//        System.out.println("四个数的最大值 : " + max2);
//
//        int max3 = stu.maxOfFive(43, 656, 87, 233, 76);
//        System.out.println("五个数的最大值 :" + max3);

        Student stu = new Student("猴子", 21, 149.5);
//        stu.setName("来来");
//        stu.setAge(21);
//        stu.setScore(145.5);
//        stu.info();
//        String str = stu.getName();
//        System.out.println("名字: " + str);
//
//        int age = stu.getAge();
//        System.out.println("年龄: " + age);
//
//        double score = stu.getScore();
//        System.out.println("分数: " + score);
//        stu.setInfo("姓名: " + "猴子", 21, 140.5);
//        stu.info();
        //可以创建时直接赋值
        Person per = new Person("妲己", 23, "女", "白羊座");
        //用setter和getter方法操作单个变量
        per.setAge(34);
        per.info();
        //也可以创建一个没有信息的对象
        Person per1 = new Person();
        //再通过setter和getter方法挨个赋值或读值
        per1.setName("露娜");
        per1.setAge(44);
        per1.setSex("女");
        per1.setConstellation("金牛座");
        per1.info();
    }

}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值