论坛一些问题的集锦<1>

1.

接口里是否能定义内部类?


1.首先可以定义,接口本身就是一个抽象类,定义一个类自然就是内部类
2.确实没必要这么干。接口中本来就只是一个标签,你定义一个内部类想实现什么功能?
我经过测试发现 在接口里面定义的一个类 ,然后类中的方法可以不是抽象的,也就是可以实现的
package com.itcast.ztr;

public class MainClass {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Test.A a=new Test.A();
                a.t();
        }

}


package com.itcast.ztr;

public interface Test {
         
                class A{
                        public void t(){
                                System.out.println("asa"); 
                        }
                        
                }
}


你可以试一试

 

2.System类中的字段是什么意思

比如,System.out.println()
out是字段,那么这个是怎么调用println方法的呢,  调用方法的格式应该是类名.方法名,中间这个out是干什么用的?

 

类相当于JAVA里面的一个类型,不要以为变量只有整型,字符串,浮点型这些,类也算是一种类型,所以这里的out其实是一个PrintStream类型,看API就明白.
而这个类里面就提供了println这个方法
package foo.bar;
class Out{ int x,y; void println()
{
System.out.println("你好啊.呵呵");
}
}
public class No1
{     Out out;//out是NO1类的成员变量,不过它是一个类类型的.
    public No1() 
   {    
     out=new Out(); 
   }    
  public static void main(String[] args)
{    
  new No1().out.println();
}
}

3.22天毕老师视频,里面的非静态方法为什么可以直接调用

package mymenu;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyMenuTest
{
private Frame f;
private MenuBar bar;
private TextArea ta;
private Menu fileMenu;
private MenuItem openItem,saveItem,closeItem;

private FileDialog openDia,saveDia;
private File file;
MyMenuTest()
{
  init();//这里~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
public void init()
{
  f = new Frame("my window");
  f.setBounds(300,100,650,600);
  bar = new MenuBar();
  ta = new TextArea();
  fileMenu = new Menu("文件");
  
  openItem = new MenuItem("打开");
  saveItem = new MenuItem("保存");
  closeItem = new MenuItem("退出");
  
  fileMenu.add(openItem);
  fileMenu.add(saveItem);
  fileMenu.add(closeItem);
  bar.add(fileMenu);
  f.setMenuBar(bar);

  openDia = new FileDialog(f,"我要打开",FileDialog.LOAD);
  saveDia = new FileDialog(f,"我要保存",FileDialog.SAVE);

  f.add(ta);
  myEvent();//这里~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  f.setVisible(true);

}
private void myEvent()
{
  saveItem.addActionListener(new ActionListener()
  {
  
   public void actionPerformed(ActionEvent e)
   {
    if(file==null)
    {
     saveDia.setVisible(true);
     String dirPath = saveDia.getDirectory();
     String fileName = saveDia.getFile();
     if(dirPath==null || fileName==null)
      return ;
     file = new File(dirPath,fileName);
    }
    try
    {
     BufferedWriter bufw  = new BufferedWriter(new FileWriter(file));
     String text = ta.getText();
     bufw.write(text);
     //bufw.flush();
     bufw.close();
    }
    catch (IOException ex)
    {
     throw new RuntimeException();
    }
    
   }
  });

  openItem.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    openDia.setVisible(true);
    String dirPath = openDia.getDirectory();
    String fileName = openDia.getFile();
//    System.out.println(dirPath+"..."+fileName);
    if(dirPath==null || fileName==null)
     return ;
    ta.setText("");
    file = new File(dirPath,fileName);
    try
    {
     BufferedReader bufr = new BufferedReader(new FileReader(file));
     String line = null;
     while((line=bufr.readLine())!=null)
     {
      ta.append(line+"\r\n");
     }
     bufr.close();
    }
    catch (IOException ex)
    {
     throw new RuntimeException("读取失败");
    }

   }
  });


  closeItem.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    System.exit(0);
   }
  });
  f.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    System.exit(0); 
   }
  });
}

public static void main(String[] args) 
{
  new MyMenuTest();
}
}
/*
如何制作可以双击执行的jar包呢?
1,将多个类封装到了一个包(package)中。
2,定义一个jar包的配置信息。
定义一个文件a.txt 。文件内容内容为:
Main-Class空格)包名.类名(回车)
3,打jar包。
jar -cvfm my.jar a.txt 包名
4,通过winrar程序进行验证,查看该jar的配置文件中是否有自定义的配置信息。
5,通过工具--文件夹选项--文件类型--jar类型文件,通过高级,定义该jar类型文件的打开动作的关联程序。
jdk\bin\javaw.exe -jar
6,双击试试!。哦了。

*/


我想了半天终于茅塞顿开了

1.主函数的  new MyMenuTest();在创建对象

2.其实这些调用都省略this  tihs关键字代表一个实例对象,可以用来调用非静态的东西
调用非静态的方法或属性需要通过对象调 用   
非静态方法可以直接调用非静态和静态的属性和方法



构造函数,是为了将一个类实例化一个对象才需要使用的,进行一些必备的初始化操作(即使你没写任何代码,实际上运行库也帮你添了很多操作)。
创建对象后 ,会调用构造方法了。

不知道这么说你能理解吗?
欢迎和我进行交流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值