1:不可变化类。java
2:单例类数组
3:枚举类多线程
4:注解 – 后面有增强ide
5:泛型-后面有增强。spa
String[]数组类的copy与增加.net
1:不可变类。线程
当一个类已经实例化之后,就不再能修改里面的成员变量的值的类。不可变类。orm
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的子类。
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
进程
一个程序在拥有一块链接的内存空间。【进程不会执行任何工做】
线程:
线程是一个程序中执行的多个子任务,一个进程中,必需要至少拥有一个线程。
多个线程共享同一个内存空间,但又独立的执行。
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();
}
}
在Java里面java.lang.Thread就是线程的类。
当运行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
}
}
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();
}
}
}
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("启动完成");
}
}
两种:
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();
}
}
}
}
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();
}
}