【java】【知识巩固】类、对象、方法、变量 调用简易例子

package test_modifier;

public class Person {
	
	static String name = "James";
	
	public static String sayHello() {
		System.out.println("hello");
		return "success";
	}
}

package test_modifier;

public class Robot {
	
	static String name = "BigBay"; 
	
	public String sayHello() {
		System.out.println("hello");
		return "success";
	}
}
package test_modifier;

/**
 * 方法调用测试
 * @author chenghan_yang
 *
 */
public class Test {

	public static void main(String[] args) {
		//(1) [类名].[方法名]  (2) [类名].[变量名](静态)
		Person.sayHello();					// (1)
		String personName = Person.name;	// (2)
		
		// (1) new 一个类的对象robot (2) [对象].[方法名]  (3) [类名].[变量名](静态)
		Robot robot = new Robot();			// (1)
		robot.sayHello();					// (2)
		String robotName = robot.name;		// (3)
		
	}
}

猜你喜欢

转载自blog.csdn.net/chenghan_yang/article/details/86566609