您的位置:首页 > 其它

类的不同形式

2015-12-08 00:00 537 查看
1:不可变化类。

2:单例类

3:枚举类

4:注解 – 后面有加强

5:泛型-后面有加强。

String[]数组类的copy与增长

1:类的不同形式:

1:不可变类。

当一个类已经实例化以后,就再也不能修改里面的成员变量的值的类。不可变类。

1:对于一个类,成员变量是私有的 。
class Person{
private String name;
2: 在构造方法里,接收成员变量值。
Public Person(String name){

This.name=name;

}

3:且这个类再也没有给出修改name的方式

getName(){

}

}

2:单例类

3:枚举类

就是使用enum声明的类,从单例变化而来的。

1:声明的成员变量只能是自己。默认的构造是私有的。

2:拥有一些方法:valueOf(…)

3:所有enum都是java.lang.Enum的子类。

@Test
public void test1() {
Gender g1 = Gender.MALE;// 从枚举里面获取一个对象
Gender g2 = Gender.MALE;
System.err.println(g1 == g2);// true
System.err.println(g1.equals(g2));// true
// 获取枚举里面的所有对象
Gender[] gs = Gender.values();
for (Gender g : gs) {
System.err.println(g);
}
// 根据一个字符串,创建一个枚举对象
String str = "male";
Gender g3 = Gender.valueOf(str.toUpperCase());
System.err.println(g3);
// 所有Enum类,都是java.lang.Enum的子类
System.err.println(g3 instanceof Gender);// true
System.err.println(g3 instanceof Enum);// true
// 还可以通过Enum类
Gender g4 = Enum.valueOf(Gender.class, str.toUpperCase());
System.err.println(g4);
}
}

class Sex {
public static final Sex MALE = new Sex();// 静态常量
public static final Sex FEMALE = new Sex();

private Sex() {

}
}

enum Gender {
MALE, FEMALE;

private Gender() {
}

}

4:注解 – 后面有加强

在编译时限制的作用。

1.public class Demo01 {

@Override
public String toString() {
return super.toString();
}

2:在运行时给反射使用 - 后面才会讲到 – Java高级特点。

注解:1:都是通过 @interface 声明的类。

2:注解在声明时应该设置所注解的范围。

3:所有注解,都是java.lang.Annotation的子类。

@Target (value={ElementType.METHOD,ElementType.TYPE})
public @interface MyTest {

}

5:泛型-后面有加强。

String[]数组类的copy与增长

不确定返回何种类型时,就可以使用这种类型:

1:声明在方法上的泛型

public class Demo03 {

@Test
public void test() {
String ss = say("Jack");
int a = say(3);
double d = say(34.9);
}

public <T> T say(T obj) {
T t = null;
System.err.println("hello...");
return obj;
}
}

2:声明在类上

class One<E> {
public E say(E e) {
System.err.println("Hello..");
return e;
}
}

类的变体:

1:枚举 enum声明的类。

2:注解类 – 通过 @interface声明的类。

---

泛型
方法上 <T>

类上

--某些特另的类:

不可变类。

Java体系的五支柱:

IO/Socket/Thread/JDBC/Coollections

2:多线程

进程

一个程序在拥有一块连接的内存空间。【进程不会执行任何工作】

线程:

线程是一个程序中执行的多个子任务,一个进程中,必须要至少拥有一个线程。

多个线程共享同一个内存空间,但又独立的执行。

package cn.demo02;

public class Demo04 {
public static void main(String[] args) {
new Thread() {
public void run() {
for (int i = 0; i < 100; i++) {
System.err.println("i is:" + i);
}
};
}.start();
new Thread() {
public void run() {
for (int j = 100; j > 0; j--) {
System.err.println("----------------------j is:" + j);
}
};
}.start();

}
}

2.1、线程类

在Java里面java.lang.Thread就是线程的类。

2.2、主线程

当运行main方法时,JVM就会会当前的Java应用程序,创建一个主线程。

获取当前线程的引用:



package cn.demo02;

public class Demo05 {
public static void main(String[] args) throws Exception {
Thread th1 = Thread.currentThread();// 获取当前正在执行的线程或是获取当前执行main方法的线程
System.err.println("1:name is:" + th1.getName());// 输 出main
say();
new Demo05().hello();

}

public static void say() {
Thread th1 = Thread.currentThread();
System.err.println("2:---name is:" + th1.getName());// main
}

public void hello() {
Thread th1 = Thread.currentThread();
System.err.println("3:---name is:" + th1.getName());//main
}
}

2.3、创建线程的方式 – new Thead()

Step:

1:开发一个类,继承Thread类。

2:重写里面的run方法,将子线程执行的方法都放到run方法里面。

3:实例化这个类,且调用它的start方法。启动线程。

package cn.demo02;

public class Demo05 {
public static void main(String[] args) throws Exception {
OneThead one = new OneThead();
one.start();
}

public static void say() {
Thread th1 = Thread.currentThread();
System.err.println("2:---name is:" + th1.getName());// Thread-0
}

public void hello() {
Thread th1 = Thread.currentThread();
System.err.println("3:---name is:" + th1.getName());// Thread-0
}

// 1:
static class OneThead extends Thread {
// 2:重写run
@Override
public void run() {
Thread th1 = Thread.currentThread();
System.err.println("<><><><><><>name is:" + th1.getName());// 子线程的取名的方式Thead-0
say();
new Demo05().hello();
}
}
}

2.4、线程的状态

1:新创建的 Thead t = new OneThead();

2:就绪状态 – t.start(); - > 准备抢cpu的使用权。

3:运行状态 – 正在执行run方法的线程。

4:阻塞状态 – 执行线程休眠或是在线程里面等待用户的输入。

5:死亡状态- - 运行完成了run方法。

package cn.demo02;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo06 {
public static void main(String[] args) {
Thread t = new Thread() {
@Override
public void run() {
// 1:声明一个时间的格式化类 2009-12-09 12:345:56
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
while (true) {
String str = sdf.format(new Date());
System.err.println(str);
//执行
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
System.err.println("启动完成");
}
}



3:线程的创建方式

两种:

1:继承 Thread类,重写run方法。

(略)

2:实现一个接口: java.lang.Runnable接口。

package cn.demo02;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo07 {
public static void main(String[] args) {
// 3:创建一个线程类
Thread t = new Thread(new OneThread());
// 4:依然是启动t
t.start();
System.err.println("Started...");
}

}

// 1:开发一个类,实现接口
class OneThread implements Runnable {
// 2:必须要实现run方法
@Override
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
while (true) {
System.err.println(sdf.format(new Date()));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

4:Thread类的源代码

Class Thread implements Runable{

/* What will be run. */
private Runnable target;

@Override
public void run() {
if (target != null) {
target.run();
}
}

}

public class Demo08 {
public static void main(String[] args) {

Runnable run = new Runnable() {
@Override
public void run() {

System.err.println("World....");

}
};

Thread t = new Thread(run) {
@Override
public void run() {
System.err.println("Hello...");
super.run();
}
};
t.start();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: