首页 > 解决方案 > 一个方法的例子是 int age;通常低于班级?

问题描述

一个方法示例是 int age;低于班级?其他评论是否正确?

public class human { //just a class? 

    //this is a method?
    int age;
    String name;
    String hairColor;
    String gender;

    public void speaking() { //this is the constructor?
        System.out.println("my name is " + name);
        System.out.println("my age is " + age + " years old");
        System.out.println("my hair color is " + hairColor);
        System.out.println("my gender is " + gender);
    }

    public static void main(String args[]) { //main is used to excute   

        human earthHuman = new human();//this is known as a object

        //instances?
        earthHuman.age = (18); 
        earthHuman.name = ("dan");
        earthHuman.hairColor = ("black brownish");
        earthHuman.gender = ("male");
        earthHuman.speaking();
    }
}

标签: java

解决方案


public class human { //Here you declare a class 

    //This is a field with package-private (default) permissions
    //might be also called a variable or a parameter
    int age; 
    String name;
    String hairColor;
    String gender;

    public human() { //This is a constructor

    }

    public void speaking() { //This is a method not a constructor
        System.out.println("my name is " + name);
        System.out.println("my age is " + age + " years old");
        System.out.println("my hair color is " + hairColor);
        System.out.println("my gender is " + gender);
    }

    //Main is the first method to be called when program executes, it is 
    //program's entry point
    public static void main(String args[]) { 
        //This is an object better known as an instance
        human earthHuman = new human();

        //These are parametes
        earthHuman.age = (18); 
        earthHuman.name = ("dan");
        earthHuman.hairColor = ("black brownish");
        earthHuman.gender = ("male");

        //Calling method speaking on earthHuman instance
        earthHuman.speaking();
    }
}

推荐阅读