「java语言源码分析」编译原理语法分析java
本篇文章给大家谈谈java语言源码分析,以及编译原理语法分析java对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java 源代码注释
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GameTest extends JFrame implements ActionListener{
/*
* 新建一个主面板(这个类可能是自定义的,本程序和API中没有)。
*/
MainPanel j=new MainPanel();
JButton jPreview;
JLabel label;
Container container;
JPanel panel;
/**
* 主函数
* @param args
*/
public static void main(String[] args) {
//运行程序
new GameTest();
}
/**
* 构造函数。
*
*/
public GameTest()
{
//新建一个标题为“拼图”的窗口
JFrame fr =new JFrame("拼图");
//获取窗口容器。
container=fr.getContentPane();
//创建菜单条
JMenuBar jMenuBar=new JMenuBar();
//以下初始化菜单,并且设置快捷键和添加监听器。
JMenu jMenuGame=new JMenu("游戏(G)");
jMenuGame.setMnemonic('g');
JMenuItem jMenuItemStart = new JMenuItem("开始(S)");
jMenuItemStart.setMnemonic('s');
jMenuItemStart.addActionListener(this);
JMenuItem jMenuItemExit=new JMenuItem("退出(E)");
jMenuItemExit.setMnemonic('e');
jMenuItemExit.addActionListener(this);
jMenuGame.add(jMenuItemStart);
jMenuGame.add(jMenuItemExit);
//初始化按钮并设置快捷键和添加监听器
JButton jChoice=new JButton("选图(X)");
jChoice.setMnemonic('x');
jChoice.addActionListener(this);
jPreview=new JButton("预览(P)");
jPreview.setMnemonic('p');
jPreview.addActionListener(this);
//将菜单和按钮添加到菜单条中
jMenuBar.add(jMenuGame);
jMenuBar.add(jChoice);
jMenuBar.add(jPreview);
//将菜单条设为该窗口的主菜单
fr.setJMenuBar(jMenuBar);
//将主面板添加到该窗口的容器中。
container.add(j);
//设置大小
fr.setSize(315,360 );
fr.setVisible(true);
//设置默认关闭方式。
fr.setDefaultCloseOperation(3);
}
/**
* 事件处理函数。
*/
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="开始(S)")
{
j.Start();
}
if(e.getActionCommand()=="预览(P)")
{
j.setVisible(false);
panel=new JPanel();
Icon icon=new ImageIcon("pictrue/pic"+"_"+MainPanel.pictureID+".jpg");
label=new JLabel(icon);
label.setBounds(300, 300, 0, 0);
panel.add(label);
panel.setSize(300, 300);
panel.setVisible(true);
this.container.add(panel);
jPreview.setText("返回(P)");
}
if(e.getActionCommand()=="返回(P)")
{
panel.setVisible(false);
j.setVisible(true);
j.repaint();
jPreview.setText("预览(P)");
}
if(e.getActionCommand()=="退出(E)")
{
System.exit(0);
}
if(e.getActionCommand()=="选图(X)")
{
//初始化选择框,并提供选择。
Choice pic = new Choice();
pic.add("七里香");
pic.add("依然范特西");
pic.add("八度空间");
pic.add("十一月的肖邦");
pic.add("魔杰座");
pic.add("叶惠美");
pic.add("我很忙");
int i=JOptionPane.showConfirmDialog(this, pic, "选择图片", JOptionPane.OK_CANCEL_OPTION);
if(i==JOptionPane.YES_OPTION)
{
//选择图片
MainPanel.pictureID=pic.getSelectedIndex()+1;
j.removeAll();
j.reLoadPicture();
j.repaint();
}
}
}
}
java源代码分析 实在是不太会,求高手教教我。
package test2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class JavaCodeAnalyzer {
public static void analyze(File file) throws IOException{
//FileOutputStream fos = new FileOutputStream("F;"+File.separator+"result.txt");
if(!(file.getName().endsWith(".txt")||file.getName().endsWith(".java"))){
System.out.println("输入的分析文件格式不对!");
}
InputStream is= new FileInputStream(file);
BufferedReader br= new BufferedReader(new InputStreamReader(is));
String temp;
int count=0;
int countSpace=0;
int countCode=0;
int countDesc=0;
MapString, Integer map = getKeyWords();
while((temp=br.readLine())!=null){
countKeys(temp, map);
count++;
if(temp.trim().equals("")){
countSpace++;
}else if(temp.trim().startsWith("/*")||temp.trim().startsWith("//")){
countDesc++;
}else{
countCode++;
}
}
System.out.printf("代码行数:"+countCode+"占总行数的%4.2f\n",(double)countCode/count);
System.out.printf("空行数:"+countSpace+"占总行数的%4.2f\n",(double)countSpace/count);
System.out.printf("注释行数:"+countDesc+"占总行数的%4.2f\n",(double)countDesc/count);
System.out.println("总行数:"+count);
System.out.println("出现最多的5个关键字是:");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
}
public static void main(String[] args) {
getKeyWords();
File file = new File("F://Test.java");
try {
analyze(file);
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static MapString,Integer getKeyWords(){
MapString,Integer map = new HashMapString, Integer();
String[]keywords = {"abstract","assert","boolean","break","byte","case","catch","char","class","continue","default","do","double","else","enum","extends","final","finally","float","for","if","implements","import","instanceof","int","interface","long","native","new","package","private","protected","public","return"," strictfp","short","static","super"," switch","synchronized","this","throw","throws","transient","try","void","volatile","while","goto","const"};
for(String s:keywords){
map.put(s, 0);
}
return map;
}
public static void countKeys(String s,MapString,Integer map){
SetString keys = map.keySet();
for(String ss:keys){
if(s.indexOf(ss)!=-1){
map.put(ss, map.get(ss)+1);
}
}
}
}
上班没啥时间了,还有点没写完,你在想想。
java工作原理
Java工作原理
由四方面组成:
(1)Java编程语言
(2)Java类文件格式
(3)Java虚拟机
(4)Java应用程序接口
当编辑并运行一个Java程序时,需要同时涉及到这四种方面。使用文字编辑软件(例如记事本、写字板、UltraEdit等)或集成开发环境(Eclipse、MyEclipse等)在Java源文件中定义不同的类 ,通过调用类(这些类实现了Java API)中的方法来访问资源系统,把源文件编译生成一种二进制中间码,存储在class文件中,然后再通过运行与操作系统平台环境相对应的Java虚拟机来运行class文件,执行编译产生的字节码,调用class文件中实现的方法来满足程序的Java API调用 。
Java大神如何学习和分析Java框架的源码
1、拿到代码查看项目当中是否有readme这样的文件,如果没有查看是否有文档之类的
2、代码当中没有文档,那么就想你的同事或者其他人要这个框架的介绍或者资料
3、先请教别人这个框架的大体思路
4、自己独立去按照文档或者其他人说的思路去看代码
5、不懂的地方全部记录下面,一次行去问,有的时候很多问题在你看到后面的东西的时候就自然明白了
6、看懂了代码之后自己尝试着写一个,看自己的理解是否正确就这么多了。
java语言源码分析的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于编译原理语法分析java、java语言源码分析的信息别忘了在本站进行查找喔。
发布于:2022-12-01,除非注明,否则均为
原创文章,转载请注明出处。