import java.util.ArrayList;
import java.util.List;
import java.util.Date;
public class ListDemo{
public class Person{
int age;
String name;
Person(){
this.age=24;
this.name="liming";
}
Person(int age){
this.age=age;
this.name="df";
}
public int getAge(){
return this.age;
}
public String getName(){
return this.name;
}
public int setAge(){
return this.age;
}
public String setName(){
return this.name;
}}
public static void main(String [] args){
List<Person> list=new ArrayList<Person>();
list.add(Person());
list.add(Person(5));
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
import java.util.List;
import java.util.Date;
public class ListDemo{
public class Person{
int age;
String name;
Person(){
this.age=24;
this.name="liming";
}
Person(int age){
this.age=age;
this.name="df";
}
public int getAge(){
return this.age;
}
public String getName(){
return this.name;
}
public int setAge(){
return this.age;
}
public String setName(){
return this.name;
}}
public static void main(String [] args){
List<Person> list=new ArrayList<Person>();
list.add(Person());
list.add(Person(5));
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
你试可以看看System.out.println(list.get(i).getName());
System.out.println(list.get(i).getAge());
public class Person{这样写就是内部类了啦 类里面除了内部类不能这样写的 根据你的意图 其实可以把类写在其他的包里面 或者写在同一个包里面 如下::package com.testClass;import java.util.ArrayList;
import java.util.List;public class ListDemo { public static void main(String[] args) {
List<Person> list = new ArrayList<Person>();
list.add(new Person());
list.add(new Person(5));
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}class Person {
int age;
String name; Person() {
this.age = 24;
this.name = "liming";
} Person(int age) {
this.age = age;
this.name = "df";
} public int getAge() {
return this.age;
} public String getName() {
return this.name;
} public int setAge() {
return this.age;
} public String setName() {
return this.name;
}}
输出如下 :
com.testClass.Person@c17164
com.testClass.Person@1fb8ee3因为你没用重写toString方法 所以打印出来是默认的hash码 你可以重写toString方法 、或者按照一楼所说的 打印具体:System.out.println(list.get(i).getName());
System.out.println(list.get(i).getAge());
import java.util.List;
import java.util.Date;
public class ListDemo{
public static class Person{ //这里加了Static
int age;
String name;
Person(){
this.age=24;
this.name="liming";
}
Person(int age){
this.age=age;
this.name="df";
}
public int getAge(){
return this.age;
}
public String getName(){
return this.name;
}
public int setAge(){
return this.age;
}
public String setName(){
return this.name;
}
}
public static void main(String [] args){
List<Person> list=new ArrayList<Person>();
list.add(new Person()); //这行加了new,Person内部类必须是Static的,静态方法只能调用 静态内部成员
list.add(new Person(5)); //这行加了new,Person内部类必须是Static的,静态方法只能调用 静态内部成员
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
楼主看对不对咯
Test.ListDemo$Person@4f1d0d
Test.ListDemo$Person@1fc4bec包名.内名.内部类名@内存地址
public class ListDemo{
public class Person{
肯定是无法编译的啊
生成对象要new 直接Person()是啥意思
直接打印对象会默认调用它的类中的toString()方法
list.add(person());
list.add(person());