静态加载类和动态加载类
本文最后更新于 3425 天前,其中的信息可能已经有所发展或是发生改变。

1、new 出来的都是静态加载类,一招出错,全盘皆输。

class Office{
  pulbic static void mian(Stirng[] args){
    if("Word".equals(args[0])){
      Word w = new Word();
      w.start();
      }
   }
}

new 对象 都是静态加载的类,在编译时就需要全部加载需要用到的类,不管你用不用到…

所以你需要有Word的类…

需要解决就要用到动态加载类..
2、动态加载类

如何动态加载类呢

我们可以通过类类型,创建该类的对象

如先创建一个接口,让他的类去实现这个接口从而实现动态加载

public interface OfficeAble {
	public void start();
}
public class OfficeBetter {
	public static void main(String[] args){
		try {
			Class c = Class.forName(args[0]);
			OfficeAble oa = (OfficeAble) c.newInstance();
			oa.start();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}
public class Word implements OfficeAble{

	public void start() {
		// TODO Auto-generated method stub
		
	}
	
}

所以以后需要加载什么类,我们都可以去创建一个这个类类型去动态加载….

Class c = Class.forName(args[0]);
OfficeAble oa = (OfficeAble)c.newInstance();//OfficeAble定义的功能类型接口

 

评论

  1. Image
    Windows Chrome 50.0.2661.102
    9 年前
    2017-4-11 16:51:57

    每次评论发信给你,老是被退信。

    • Image
      博主
      灰狼
      Windows Chrome 50.0.2661.102
      9 年前
      2017-4-11 17:04:59

      我也不清除阿,用360加速,然后腾讯的邮件就收不到

  2. Image
    Macintosh Safari 10.1
    9 年前
    2017-4-14 13:45:11

    <?php echo 'php大法好';

    • Image
      关关
      Windows Chrome 50.0.2661.102
      9 年前
      2017-4-15 23:39:58

      System.out.println(“java代码需要成吨的写”);

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇