楼主,把下面的代码复制,运行一下看看。
- package demo;
- /**
- * @author 小媛
- *
- */
- public class MainTest {
- public static void main(String[] args) {
- Person person = new Person("Tom", 25);
- person.eat();
- }
- }
复制代码
- package demo;
- /**
- * @author 小媛
- *
- */
- public class Person {
-
- private String name;
- private int age;
-
- static{
- System.out.println("静态代码块");
- }
- public Person() {
- super();
- System.out.println("无参构造函数");
- }
-
- public Person(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- System.out.println("有参构造函数");
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
-
-
- public void eat(){
- System.out.println("person类的eat方法");
- }
- }
复制代码 |