如何实例化Java内部类
/***@author2011301510060**/importjava.util.Scanner;publicclassPeople{privateStringszN...
/**
* @author 2011301510060
*
*/
import java.util.Scanner;
public class People
{
private String szName;
private String szSex;
private int age;
private int height;
private int weight;
static int sum = 0;
public People(){}
public People(String _name, String _sex, int _age, int _height, int _weight)
{
this.szName = _name;
this.szSex = _sex;
this.age = _age;
this.height = _height;
this.weight = _weight;
}
public static void setSum(int _sum) {
sum = _sum ;
System.out.println("sum = "+sum);
}
public void setName(String _name) {
szName = _name;
}
public String getName() {
return this.szName;
}
public void setSex(String _sex) {
szSex = _sex;
}
public String getSex() {
return this.szSex;
}
public void setAge(int _age) {
age = _age;
}
public int getAge() {
return this.age;
}
public void setHeight(int _height) {
height = _height;
}
public int getHeight() {
return this.height;
}
public void setWeight(int _weight) {
weight = _weight;
}
public int getWeight() {
return this.weight;
}
class Hobby
{
public void showAllinformation(People one) {
System.out.println("sum = "+sum);
System.out.println("name:"+one.getName());
System.out.println("sex:"+one.getSex());
}
public Hobby()
{
System.out.println("i am a inner class!");
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
People me = new People();
People one = new People();
People.Hobby hobby = one.new Hobby(); ///////////////////////// 这里报错
}
} 展开
* @author 2011301510060
*
*/
import java.util.Scanner;
public class People
{
private String szName;
private String szSex;
private int age;
private int height;
private int weight;
static int sum = 0;
public People(){}
public People(String _name, String _sex, int _age, int _height, int _weight)
{
this.szName = _name;
this.szSex = _sex;
this.age = _age;
this.height = _height;
this.weight = _weight;
}
public static void setSum(int _sum) {
sum = _sum ;
System.out.println("sum = "+sum);
}
public void setName(String _name) {
szName = _name;
}
public String getName() {
return this.szName;
}
public void setSex(String _sex) {
szSex = _sex;
}
public String getSex() {
return this.szSex;
}
public void setAge(int _age) {
age = _age;
}
public int getAge() {
return this.age;
}
public void setHeight(int _height) {
height = _height;
}
public int getHeight() {
return this.height;
}
public void setWeight(int _weight) {
weight = _weight;
}
public int getWeight() {
return this.weight;
}
class Hobby
{
public void showAllinformation(People one) {
System.out.println("sum = "+sum);
System.out.println("name:"+one.getName());
System.out.println("sex:"+one.getSex());
}
public Hobby()
{
System.out.println("i am a inner class!");
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
People me = new People();
People one = new People();
People.Hobby hobby = one.new Hobby(); ///////////////////////// 这里报错
}
} 展开
5个回答
展开全部
假设外部类叫Out,内部类叫In,那么我们可以使用Out.In in = new Out().new In()来实例化内部类的对象,具体示例代码如下:
class Out {
private int age = 12;
class In {
private int age = 13;
public void print() {
int age = 14;
System.out.println("局部变量:" + age);
System.out.println("内部类变量:" + this.age);
System.out.println("外部类变量:" + Out.this.age);
}
}
}
public class Demo {
public static void main(String[] args) {
Out.In in = new Out().new In();
in.print();
}
}
class Out {
private int age = 12;
class In {
private int age = 13;
public void print() {
int age = 14;
System.out.println("局部变量:" + age);
System.out.println("内部类变量:" + this.age);
System.out.println("外部类变量:" + Out.this.age);
}
}
}
public class Demo {
public static void main(String[] args) {
Out.In in = new Out().new In();
in.print();
}
}
2018-07-27 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
内部类的实例化方式如下例子所示:
public class Animal {
class Head{
}
}
测试如何实例化内部类:
public class TestInstanceInnerClass {
public TestInstanceInnerClass() {
Animal animal=new Animal();//首先实例化外部类
Head head=animal.new Head();//把内部类当成一个成员变量进行实例化
}
}
public class Animal {
class Head{
}
}
测试如何实例化内部类:
public class TestInstanceInnerClass {
public TestInstanceInnerClass() {
Animal animal=new Animal();//首先实例化外部类
Head head=animal.new Head();//把内部类当成一个成员变量进行实例化
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把Hobby类提取出来,当作一个类,在People类的属性中添加一个Hobby类的对象
追问
我想像上面那样实例化。其他的方法暂且不考虑。thanks
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
怎么会报错呢?
下面这2种都可以:
People one = new People();
People.Hobby hobby = one.new Hobby();
Hobby bby = one.new Hobby();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询