| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 624 人关注过本帖
标题:求大神帮忙改改程序,出现无法从静态上下文中引用非静态变量this不会改
取消只看楼主 加入收藏
dengyangkll
Rank: 1
等 级:新手上路
帖 子:5
专家分:3
注 册:2013-10-8
结帖率:100%
收藏
已结贴  问题点数:5 回复次数:3 
求大神帮忙改改程序,出现无法从静态上下文中引用非静态变量this不会改



public class Application
{
    public static void main(String[] args){
   
        Simulator simulator=new Simulator();
        simulator.playSound( new Dog());
        simulator.playSound(new Cat());
    }
    public abstract class Animal
    {
        public abstract String getName();
        public abstract void cry();
        }
public class Simulator
{
    public void playSound(Animal animal){
System.out.println("现在播放的是"+animal.getName()+"类的声音");
animal.cry();
    }
}
public class Dog extends Animal
{
    public void cry()
    {
        System.out.println("汪汪~~~~汪汪");
    }
    public String getName()
    {
        return "狗";
    }

}
public class Cat extends Animal
{
    public void cry()
    {
    System.out.println("喵喵~~~~喵喵");
    }
    public String getName()
    {
    return "猫";
    }
    }

    }


搜索更多相关主题的帖子: simulator abstract public animal 上下文 
2013-10-19 18:56
dengyangkll
Rank: 1
等 级:新手上路
帖 子:5
专家分:3
注 册:2013-10-8
收藏
得分:0 
我是刚刚开始学真的不知道如何分哦 !求指导哦!
2013-10-22 16:49
dengyangkll
Rank: 1
等 级:新手上路
帖 子:5
专家分:3
注 册:2013-10-8
收藏
得分:0 
回复 3楼 java小蚂蚁
不包括在内的话错误更多!会报每个类都是公共的需要声明,后来我在每个类中加了一个static就没错误了,运行结果也正确但不知道是什么原因,求指导告诉是为什么哦 !
2013-10-22 16:54
dengyangkll
Rank: 1
等 级:新手上路
帖 子:5
专家分:3
注 册:2013-10-8
收藏
得分:0 
回复 7楼 gmh0421
好像因该是吧!我也不清楚哦 !
2013-10-23 01:10
快速回复:求大神帮忙改改程序,出现无法从静态上下文中引用非静态变量this不会改 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011705 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved