Java Private修饰符

2022-02-06 13:21:49 浏览数 (1)

代码语言:java复制
package com.binbin.test;

public class Private {
	public static void main(String[] args) {
		Dog dog = new Dog();
		dog.setAge(9);
		dog.name = "大黄";
		dog.info();

	}
}

class Dog {
	String name;
	private int age;

	public void setAge(int a) {
		if (a <= 0 || a > 10) {
			age = 0;
		} else {
			age = a;
		}
	}
	
	public int getAge(){
		return age;
	}

	public void info() {
		System.out.println("Hi~ ~,我的名字叫:"   name   ",我今年"   age   "岁了!");
	}
}

运行结果运行结果

0 人点赞