Java开发简要

  1. Java是一种面向对象的跨平台语言。
    1. 计算机高级语言编程类型分为编译型和解释型,而java是一种半编译、半解释型的语言
    2. 过程说明:
      1. 编译过程: Java源码(.java) --编译--  字节码文件(.class)
      2. 运行过程(JVM): 字节码文件(.class)--java解释器加载、校验、解释、执行--  对应平台的指令集 
  2. java体系架构
    1. JAVA EE:java企业版
    2. JAVA SE:java标准版
    3. JAVA ME:java移动版
  3. eclipse快捷键:
    1. Alt+/:自动补全
    2. Ctrl+1:自动修复
    3. Ctrl+/:注释一行
    4. Ctrl+Shift+/:注释代码段
    5. Alt+上下键:移动一行代码
    6. Ctrl+Alt+上下键:复制一行代码
    7. Ctrl+Shift+f:整理代码 
  4. 用记事本写一个“hello world” java小程序
    public class Test{
    
        public static void main(String[] args){
            
            system.out.println("hello world");
        }
    }
  5.  java的面向对象特性
    1. 封装:
      1. 类和对象
        1. 对象:实例化的类,用来描述某一具体事物的特征,调用类时,需要创建对象(new关键字)
          1. 关键字:new(创建对象)、instanceof(比较)
        2. 类:对同一类事物的抽象,用来描述事物的特征,包含属性和方法
          1. 属性:用来描述对象的静态特征,定义的变量是成员变量
            1. 关键字:private(只能在当前类中直接访问)
          2. 方法
            1. 一般方法:用来描述对象的动态特征,定义的变量是局部变量
              1. 关键字:final(不能被重写)、abstract(抽象方法)
            2. 构造方法:初始化对象的方法,方法名和类名相同,没有返回值类型
          3. 关键字:final(最终类,不能被继承)、abstract(抽象类)
    2. 继承
      1. 定义:子类继承父类的属性和方法,同时扩展自己的属性和方法
      2. 关键字:extends(单继承)
      3. 特性
        1. 重写:在子类中实现与父类相同的方法
        2. 重载:在同一个类中,方法名相同,参数列表不同
          1. 参数类型不同
          2. 参数顺序不同
          3. 参数个数不同
      4. 接口:一种特殊的抽象类,只有属性和方法的定义,没有属性和方法的实现
        1. 关键字:interface(定义接口)、implements(实现接口)
        2. 实现
          1. 一个类可实现多个无关的接口
          2. 多个不同的类可实现同一个接口
    3. 多态
      1. 定义:在父类中创建子类对象,调用子类中重写的属性和方法,但不能调用子类中特有的属性和方法
        public class Test{
           
            pubic static void main(String[] args){
                
                Test temp = new Aim();
                temp.aim();//调用子类中的方法
            }
        
            class Aim{
          
                public void aim(){
        
                    system.out.println("hello world!");
                }
            }
        }
        
        
        
            class Pn extends Aim{
           
                 public void aim(){
        
                    system.out.println("hello good!");
                 }
            }
        }
        
        

         

  6. 注释

    1. 单行注释://----------

    2. 多行注释:/*--------*/

    3. 文档注释:/**---@----*/

  7. 关键字

    JAVA关键字

    部分重要的关键字

    1. static:当前类的所有对象共用static修饰的成员变量或方法,也通过类名可以直接调用

    2. final:修饰属性(成员变量),变常量;修饰一般方法,不能被重写;修饰类,不能被继承

    3. abstract:修饰一般方法时,没有方法的实现,类也必须定义为抽象类,只有在子类中具体化抽象方法才能被创建对象

    4. interface:定义接口

    5. implments:实现一个或多个接口

    6. instanceof:判断当前对象是否为该类或该类的父类以及接口,是返回 true,不是返回false

    7. super:访问父类中的属性和方法

    8. this:实例化当前类中的属性和方法

  8. 数据类型

    1. 基本数据类型

      1. 整型:byte、short、int、long(L)

      2. 浮点型:float(f)、double

      3. 字符型:char

      4. 布尔型(boolean):true、false

    2. 引用数据类型:类、接口、数组

  9. 变量作用范围和默认初值

    1. 成员变量:全局有效,系统指定默认值

      成员变量初值

       

    2. 局部变量:只在方法或代码块中有效,需初始化值

  10. 内部类:类的嵌套定义

    1. 访问权限:内部内可以访问外部类的属性和方法,只有内部类的属性和方法实例化,外部类才能访问

    2. 实例化:

      1. Outer.Inner in=new Outer().new Inner();//不要忘记第二个new

      2. Outer.Inner in=new Outer.Inner();//内部类用static修饰时,可直接调用

  11. 多线程:在一个进程中有多个顺序流同时执行

    1. Thread类:执行线程的类

      Thread(Runnable target, String name) //target:run接口对象 name:线程名称
    2. run方法:Runable接口中定义的方法,如果该线程是使用独立的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run 方法

    3. 创建新执行线程

      1. 将类声明为 Thread 的子类,该子类应重写 Thread 类的 run 方法。

        public class Test {
        
        	public static void main(String[] args) {
                
                MyThread t1 = new MyThread();
        		MyThread t2 = new MyThread();
        		MyThread t3 = new MyThread();
        		MyThread t4 = new MyThread();
        		
        		t1.setName("窗口1: ");
        		t2.setName("窗口2: ");
        		t3.setName("窗口3: ");
        		t4.setName("窗口4: ");
        		
        		t1.start();
        		t2.start();
        		t3.start();
        		t4.start();	
        	}
            class MyThread extends Thread {
        
        	public void run(){
        		
        		int i = 100;
        		
        		while(i>0){
        			
        			System.out.println(Thread.currentThread().getName()+"售出第"+i--+"张票。");
        		    }
        	    }
            }
        }

         

      2. 声明实现 Runnable 接口的类,然后实现 run 方法。

        public class Test {
        
        	public static void main(String[] args) {
        		
        		MyRunnable myRunnable = new MyRunnable();
        		
        		new Thread(myRunnable,"窗口1: ").start();
        		
        		Thread t2 = new Thread(myRunnable,"窗口2: ");
        	
        		t2.start();
        		
        		new Thread("窗口3: "){
        			
        			int i = 100;
        			
        			@Override
        			public void run() {
        				// TODO Auto-generated method stub
        				
        				while(true){
        					
        					synchronized (this) {
        						
        						if(i>0){
        							
        							try {
        								Thread.sleep(61);
        								System.out.println(Thread.currentThread().getName()+"售出第 "+i-- +"张票。");
        							} catch (InterruptedException e) {
        								// TODO Auto-generated catch block
        								e.printStackTrace();
        							}
        						}else{
        							
        							break;
        						}
        					}
        					
        				}
        			}
        		}.start();
            }
        
            class MyRunnable implements Runnable{
        
        	int i = 100;
        	
        	@Override
        	public void run() {
        		// TODO Auto-generated method stub
        		
        		while(true){
        			
        			synchronized (this) {
        				
        				if(i>0){
        					
        					try {
        						Thread.sleep(61);
        						System.out.println(Thread.currentThread().getName()+"售出第 "+i--             +"张票。");    
        					    } catch (InterruptedException e) {
        						    // TODO Auto-generated catch block
        						    e.printStackTrace();
        					    }
        				        }else{
        					
        					        break;
        				        }
        			        }		
        		        }
                	}
                }
            }
        }

         

  12. 异常(Exception)

    1. 运行时异常:继承于RuntimeException,Java编译器允许程序不对它们做出处理。

      1. ArithmeticException:数学计算异常

      2. NullPointerException:空指针异常

      3. NegativeArraySizeException:负数组长度异常

      4. ArrayOutOfBoundsException:数组索引越界异常

      5. ClassNotFoundException:类文件未找到异常

      6. ClassCastException:造型异常

    2. 非运行时异常:    除了运行时异常之外的其他由Exception继承来的异常。

      1. FileNotFoundException:文件未找到异常

      2. EOFException:读写文件尾异常

      3. MalformedURLException:URL格式错误异常

      4. SocketException:Socket异常

      5. IOException

  13. String类

    public class Test {
    
    	public static void main(String[] args) {
    		
    		String str1 = new String("hello");
    		String str2 = new String("hello");
    		
    		String str3 = "hello";
    		String str4 = "hello";
    		
    		System.out.println(str1==str2);
    		System.out.println(str3==str4);
    		System.out.println(str1.equals(str2));
    		System.out.println(str3.equals(str1));
    		System.out.println(str3.equals(str4));
    	
    		String s = "hello ";
    		String ss = "how are you ?";
    		String sss = "    he  llo   ";
    		System.out.println("从指定位置查找第一个字符l的位置: "+s.indexOf('l',0));
    		System.out.println("查找最后一个字符l的位置: "+s.lastIndexOf('l'));
    		System.out.println("截取指定位置的字符串: "+s.substring(1, 4));
    		System.out.println("字符串拼接: "+s.concat(ss));
    		System.out.println("替换指定字符: "+s.replace('e', 'a'));
    		System.out.println(sss.trim());//去掉指定空格
    	}
    }

     

  14. 对象集合的接口

    1. Collection  抽象的集合

    2. Set  Collection的子接口,一个无序无重复集合

    3. List  Collection的子接口,一个有序可重复集合

  15. IO

    1. IO流

      1. 输入流:inputStream、Writer

      2. 输出流:outputStream、Reader

      3. 文件字符流:FileReader、FileWriter

      4. 文件字节流:FileInputStream、FileOutputStream

      5. 缓冲流:BufferedInputStream、BufferedOutputStream

    2. 复制文件
  16. public class Util {
    /**
     * cpFile1复制文本文件
     * cpFile2复制图片和音频文件
     * @param startFile 要复制的文件
     * @param endFile	复制后生成的文件
     */
    	public static void cpFile1(String startFile, String endFile) {
    
    		BufferedReader bufferedReader = null;
    		BufferedWriter bufferedWriter = null;
    		
    		try {	
    			bufferedReader = new BufferedReader(new FileReader(new File(startFile)));
    			bufferedWriter = new BufferedWriter(new FileWriter(new File(endFile)));		 
    			String buf = null;
    			while((buf = bufferedReader.readLine())!= null) {				
    				bufferedWriter.write(buf);
    				bufferedWriter.newLine();
    			}
    		} catch (FileNotFoundException e){
    			// TODO Auto-generated catch block	
    			e.printStackTrace();		
    		} catch (IOException e) {
    			// TODO Auto-generated catch block	
    			e.printStackTrace();		
    		}finally {		
    			try {			
    				bufferedReader.close();
    				bufferedWriter.close();			
    			} catch (IOException e) {
    				// TODO Auto-generated catch block				
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	public static void cpFile2(String startFile, String endFile) {
    		
    		BufferedInputStream bufferedInputStream = null;
    		BufferedOutputStream bufferedOutputStream = null;
    		
    		try {	
    			bufferedInputStream = new BufferedInputStream(new FileInputStream(new File(startFile)));
    			bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(endFile)));	
    			int i = 0;			
    			while((i = bufferedInputStream.read()) != -1){
    				bufferedOutputStream.write(i);
    			}
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}finally{			
    			try {
    				bufferedInputStream.close();
    				bufferedOutputStream.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    }

    暂无

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值