计划
13到17章的复习
13.字符串
14.类型信息
15.泛型
16.数组
17.容器深入研究
复习结果
字符串
重构
//包名
package javafuxi3;
/**
* Concatenation 类
* @author xinjie
*
*/
public class Concatenation{
/**
* main() 方法
* @param String[] args
* 返回值为空
*/
public static void main(String[] args){
//参数赋值
String mango = "mango";
//参数赋值
String s = "abc" + mango + "def" + 47;
//输出
System.out.println(s);
}
}
Formatter类
//调用包
import java.io.*;
//调用包
import java.util.*;
/**
* Turtle 类
* @author xinjie
*
*/
public class Turtle {
//定义私有参数
private String name;
//定义私有参数
private Formatter f;
/**
* Tutle()方法
* @param String name Formatter f
*/
public Turtle(String name, Formatter f){
//本类name引用 name
this.name = name;
//本类f引用 f
this.f = f;
}
/**
* move()方法
* @param int x int y
*
*/
public void move(int x, int y){
//调用方法
f.format("%s The Turtle is at (%d %d)\n" , name, x, y);
}
/**
* main()方法
* @param String[] args
* 返回值为空
*/
public static void main(String[] args){
//引用
PrintStream outAlias = System.out;
//创建实例化对象
Turtle tommy = new Turtle("Tommy",new Formatter(System.out));
//创建实例化对象
Turtle terry = new Turtle("Terry", new Formatter(outAlias));
//调用方法
tommy.move(0, 0);
//调用方法
tommy.move(4, 7);
//调用方法
tommy.move(3, 4);
//调用方法
tommy.move(2, 5);
//调用方法
tommy.move(3, 3);
//调用方法
tommy.move(3, 3);
}
}
Formatter转换
/调用包
import java.math.*;
//调用包
import java.util.*;
/**
* Conversion 类
* @author xinjie
*
*/
public class Conversion{
/**
* main() 方法
* @param String[] args
* 返回值为空
*/
public static void main(String[] args){
//创建实例化对象
Formatter f = new Formatter(System.out);
//定义参数
char u = 'a';
//输出
System.out.println("u = 'a'");
//调用方法
f.format("s: %s\n", u);
//调用方法
f.format("c: %c\n", u);
//调用方法
f.format("b: %b\n", u);
//调用方法
f.format("h: %h\n", u);
//定义参数
int v = 121;
//输出
System.out.println("v = 121");
//调用方法
f.format("d: %d\n", v);
//调用方法
f.format("c: %c\n", v);
//调用方法
f.format("d: %d\n", v);
//调用方法
f.format("b: %b\n", v);
//调用方法
f.format("s: %s\n", v);
//调用方法
f.format("x: %x\n", v);
//调用方法
f.format("h: %h\n", v);
//创建实例化对象
BigInteger w = new BigInteger("50000000000000");
//输出
System.out.println("w = new BigInteger(\"50000000000000\")");
//调用方法
f.format("d: %d\n", w);
//调用方法
f.format("b: %b\n", w);
//调用方法
f.format("s: %s\n", w);
//调用方法
f.format("x: %x\n", w);
//调用方法
f.format("h: %h\n", w);
//定义参数
double x = 179.543;
//输出
System.out.println("x = 179.543");
//调用方法
f.format("b: %b\n", x);
//调用方法
f.format("s: %s\n", x);
//调用方法
f.format("f: %f\n", x);
//调用方法
f.format("e: %e\n", x);
//调用方法
f.format("h: %h\n", x);
//创建实例化对象
Conversion y = new Conversion();
//输出
System.out.println("y = new Conversion");
//调用方法
f.format("b: %b\n", y);
//调用方法
f.format("s: %s\n", y);
//调用方法
f.format("h: %h\n", y);
//定义参数
boolean z = false;
//输出
System.out.println("z = false");
//调用方法
f.format("b: %b\n", z);
//调用方法
f.format("s: %s\n", z);
//调用方法
f.format("h: %h\n", z);
}
}
正则表达式
/**
* IntegerMatch 类
* @author xinjie
*
*/
public class IntegerMatch {
/**
* main() 方法
* @param String[] args
* 返回值为空
*/
public static void main(String[] args){
//输出
System.out.println("-1234".matches("-?\\d+"));
//输出
System.out.println("5678".matches("-?\\d+"));
//输出
System.out.println("+911".matches("-?\\d+"));
//输出
System.out.println("+911".matches("(-|\\+)?\\d"));
}
}
//调用包
import java.util.*;
/**
* Splittiing 类
* @author xinjie
*
*/
public class Splittiing {
//定义参数
public static String knights =
"Then , when you have found the shrubbery, you must" +
"cut down the mightiest tree in the forest. . ." +
"with. . .a herring!";
/**
* split() 方法
* @param String regex
* 返回值为空
*/
public static void split(String regex){
//输出
System.out.println(Arrays.toString(knights.split(regex)));
}
/**
* main() 方法
* @param String[] args
* 返回值为空
*/
public static void main(String[] args){
//按空格来划分字符串
split(" ");
//非单词字符
split("\\W+");
//字母n后面跟着一个或多个非单词字符
split("n\\W+");
}
}
//调用包
import java.util.regex.*;
/**
* ReFlags 类
* @author xinjie
*
*/
public class ReFlags {
/**
* main() 方法
* @param String[] args
* 返回值为空
*/
public static void main(String[] args){
//引用
Pattern p = Pattern.compile("^java", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
//定义参数
Matcher m = p.matcher(
"java has regex\nJava has regex\n" +
"JAVA has pretty good regular expressions\n" +
"Regular expressions are in java");
//循环到m.find为止
while(m.find())
//输出
System.out.println(m.group());
}
}
类型信息
1.RTTI,假设编译时已经知道了所有的类型
//调用包
import java.util.*;
//定义接口
interface Shape {
/**
* draw()方法
* 返回值为空
*/
void draw();
}
/**
* Circle 类
* @author xinjie
* 实现接口
*/
class Circle implements Shape {
/**
* draw() 类
* 返回值为空
*/
public void draw() {
//输出
System.out.println("Circle.draw()");
}
}
/**
* Square 类
* @author xinjie
* 实现接口
*/
class Square implements Shape {
/**
* draw()方法
* 返回值为空
*/
public void draw() {
//输出
System.out.println("Square.draw()");
}
}
/**
* Triangle 类
* @author xinjie
* 实现接口
*/
class Triangle implements Shape {
/**
* draw()方法
* 返回值为空
*/
public void draw() {
//输出
System.out.println("Triangle.draw()");
}
}
/**
* Shapes 类
* @author xinjie
*
*/
public class Shapes {
/**
* main() 方法
* @param String[] args
* 返回值为空
*/
public static void main(String[] args) {
//创建实例化对象
Vector s = new Vector();
//创建实例化对象
s.addElement(new Circle());
//创建实例化对象
s.addElement(new Square());
//创建实例化对象
s.addElement(new Triangle());
//创建实例化对象
Enumeration e = s.elements();
//循环到e.hasMoreElements()为止
while(e.hasMoreElements())
//调用方法
((Shape)e.nextElement()).draw();
}
}
2.反射机制,在运行时发现和使用类的信息
/**
* AddressBean 类
* @author xinjie
*/
public class AddressBean {
//定义私有参数
private String street;
//定义私有参数
private String zip;
/**
* getZip()方法
* 返回值为zin
*/
public String getZip() {
return zip;
}
/**
* setZip()方法
* @param String zip
* 返回值为空
*/
public void setZip(String zip) {
//引用成员变量
this.zip = zip;
}
/**
* getStreet()方法
* 返回值为street
*/
public String getStreet() {
return street;
}
/**
* setStreet()方法
* @param String street
* 返回值为空
*/
public void setStreet(String street) {
//引用成员变量
this.street = street;
}
}
/**
* UserBean 类
* @author xinjie
*
*/
public class UserBean {
//定义私有参数
private String name;
//定义私有参数
private String nick;
//定义私有参数
private AddressBean address;
//定义私有参数
private int age;
/**
* getAge()方法
* 返回值为this.age
*/
public int getAge(){
//返回到引用该成员变量
return this.age;
}
/**
* setAge()方法
* @param int age
* 返回值为空
*/
public void setAge(int age){
//引用成员变量
this.age = age;
}
/**
* getName()方法
* 返回值为name
*/
public String getName() {
return name;
}
/**
* setName()方法
* @param String name
* 返回值为空
*/
public void setName(String name) {
//引用成员变量
this.name = name;
}
/**
* getNick()方法
* 返回值为nick
*/
public String getNick() {
return nick;
}
/**
* setNick()方法
* @param String nick
* 返回值为空
*/
public void setNick(String nick) {
//引用成员变量
this.nick = nick;
}
/**
* getAddress()方法
* 返回值为 address
*/
public AddressBean getAddress() {
return address;
}
/**
* setAddress()方法
* @param AddressBean address
*/
public void setAddress(AddressBean address) {
//引用成员变量
this.address = address;
}
}
//调用包
import java.lang.reflect.Method;
/**
* BeanParser 类
* @author xinjie
*
*/
public class BeanParser {
/**
* getMetthodName()方法
* @param String property String prefix
* 返回值为methodName
*/
private static String getMethodName(String property, String prefix){
//定义参数
String prop = Character.toUpperCase(property.charAt(0))+property.substring(1);
//定义参数
String methodName = prefix + prop;
return methodName;
}
/**
* pares()方法
* @param Object bean String expr
* 返回值为result
*/
private static Object parse(Object bean, String expr){
//引用
Class beanClass = bean.getClass();
//初始化
Method method = null;
//初始化
Object result = null;
//如果有异常
try{
method = beanClass.getMethod(getMethodName(expr, "get"), new Class[]{});
result = method.invoke(bean, new Object[]{});
//在这里捕捉
}catch(Exception e){
//输出
System.out.println(e.getMessage());
}
return result;
}
/**
* doparse()方法
* @param Object String expr
* 返回值为 obj
*/
public static Object doParse(Object bean, String expr){
//引用
String keys[] = expr.split("\\.");
//初始化
Object obj = null;
//for循环语句
for(int i = 1; i < keys.length;i++){
//引用
obj = parse(bean, keys[i]);
//引用
bean = obj;
}
return obj;
}
/**'
* main()方法
* @param Sreing[] args
* 返回值为空
*/
public static void main(String[] args){
//创建实例化
UserBean bean = new UserBean();
//调用方法
bean.setName("John Abruzzi");
//调用方法
bean.setNick("Abruzzi");
//调用方法
bean.setAge(24);
//创建实例化对象
AddressBean addr = new AddressBean();
//调用方法
addr.setZip("0086");
//调用方法
addr.setStreet("Bell Street #12");
//调用方法
bean.setAddress(addr);
//输出
System.out.println(BeanParser.doParse(bean, "bean.address.street"));
//输出
System.out.println(BeanParser.doParse(bean, "bean.address.zip"));
//输出
System.out.println(BeanParser.doParse(bean, "bean.name"));
//输出
System.out.println(BeanParser.doParse(bean, "bean.nick"));
//输出
System.out.println(BeanParser.doParse(bean, "bean.age"));
}
}
3.class对象
/**
* MyClass 类
* @author Administrator
*
*/
public class MyClass {
/**
* main()方法
* @param String[] args
* 返回值为空
*/
public static void main(String[] args) {
//定义参数
String name = "ZhuJun";
//引用
Class c = name.getClass();
//输出
System.out.println("getName: " + c.getName());
//输出
System.out.println("isInterface: " + c.isInterface());
//输出
System.out.println("isPrimitive: " + c.isPrimitive());
//输出
System.out.println("isArray: " + c.isArray());
//输出
System.out.println("SuperClass: " + c.getSuperclass().getName());
}
}
泛型
泛型接口
//在接口定义泛型
interface Info<T>{
/**
* getVar()方法
*/
public T getVar() ;
}
/**
* InfoImpl 类
* @author xinjie
* 实现接口
*/
class InfoImpl<T> implements Info<T>{
// 定义属性
private T var ;
// 通过构造方法设置属性内容
public InfoImpl(T var){
//引用成员变量
this.setVar(var) ;
}
/**
* setVar()方法
* @param T var
* 返回之外i额空
*/
public void setVar(T var){
//引用成员变量
this.var = var ;
}
/**
* getVar()方法
*/
public T getVar(){
//返回值为this.var
return this.var ;
}
}
/**
* Coffee 类
* @author xinjie
*
*/
public class Coffee{
/**
* main()方法
* @param String[] arsg
* 返回值为空
*/
public static void main(String arsg[]){
// 声明接口对象
Info<String> i = null;
// 通过子类实例化对象
i = new InfoImpl<String>("汤姆") ;
//输出
System.out.println("内容:" + i.getVar()) ;
}
}
泛型方法
/**
* GenericMethods 类
* @author xinjie
*
*/
public class GenericMethods {
/**
* f()方法
* @param T x
* 返回值为空
*/
public<T> void f(T x){
//输出
System.out.println(x.getClass().getName());
}
/**
* main()方法
* @param String[] args
* 返回值为空
*/
public static void main(String[] args){
//创建实例化
GenericMethods gm = new GenericMethods();
//调用方法
gm.f(" ");
//调用方法
gm.f(1);
//调用方法
gm.f(1.0);
//调用方法
gm.f(1.0f);
//调用方法
gm.f('c');
//调用方法
gm.f(gm);
}
}
泛型数组
/**
* GenericsDemo30 类
* @author xinjie
*
*/
public class GenericsDemo30{
/**
* main()方法
* @paramString[] args
* 返回值为空
*/
public static void main(String args[]){
// 返回泛型数组
Integer i[] = fun1(1,2,3,4,5,6) ;
//赋值给数组
fun2(i) ;
}
// 接收可变参数
public static <T> T[] fun1(T...arg){
// 返回泛型数组
return arg ;
}
// 接收可变参数
public static <T> void fun2(T param[]){
//输出
System.out.print("接收泛型数组:") ;
//for循环语句
for(T t:param){
//输出
System.out.print(t + "、") ;
}
}
}
数组
返回一个数组
//调用包
import java.util.*;
/**
* IceCream 类
* @author xinjie
*
*/
public class IceCream {
//创建实例化
private static Random rand = new Random(47);
//定义参数
static final String[] FLAVORS = {
"Chocolate", "Strawberry", "Vanilla Fudge Swirl",
"Mint Chip", "Mocha Almond Fudge", "Rum Raisin",
"Praline Cream", "Mud Pie"
};
/**
* flavorSet() 方法
* @param int n
* 返回值为results
*/
public static String[] flavorSet(int n){
//if语句
if(n > FLAVORS.length)
//新建一个异常对象
throw new IllegalArgumentException("Set too big");
//创建实例化对象
String[] results = new String[n];
//创建实例化对象
boolean[] picked = new boolean[FLAVORS.length];
//for循环语句
for(int i = 0; i < n; i++){
//一定参数
int t;
//循环
do
//引用
t = rand.nextInt(FLAVORS.length);
//循环条件
while(picked[t]);
//引用
results[i] = FLAVORS[t];
//引用
picked[t] = true;
}
//返回值
return results;
}
/**
* main()方法
* @param String[] args
* 返回值为空
*/
public static void main(String[] args){
//for循环语句
for(int i = 0; i < 7; i++)
//输出
System.out.println(Arrays.toString(flavorSet(3)));
}
}
多维数组
//调用包
import java.util.Arrays;
/**
* MultidmensionalPrimitiveArray 类
* @author xinjie
*
*/
public class MultidmensionalPrimitiveArray {
/**
* main()方法
* @param Sreing[] args
* 返回值为空
*/
public static void main(String[] args){
//定义二位数组
int[][] a = {
{1, 2, 3,},
{4, 5, 6,},
};
//输出
System.out.println(Arrays.deepToString(a));
}
}
容器深入研究
填充容器
//调用包
import java.util.*;
/**
* StringAddress 类
* @author xinjie
*
*/
class StringAddress{
//定义私有参数
private String s;
/**
* StringAddress 类
* @param String s
*/
public StringAddress(String s) {this.s = s;}
/**
* toString()方法
* 返回值为super.toString() + " " + s
*/
public String toString(){
return super.toString() + " " + s;
}
}
/**
* FillingLists 类
* @author xinjie
*
*/
public class FillingLists {
/**
* main()方法
* @param String[] args
* 返回值为空
*/
public static void main(String[] args){
//创建实例化对象
List <StringAddress> list = new ArrayList<StringAddress>(
Collections.nCopies(4, new StringAddress("Hello")));
//输出
System.out.println(list);
//调用方法
Collections.fill(list, new StringAddress("World"));
//输出
System.out.println(list);
}
}
今天的单词
new if else
11月5号学习(第3天)
猜你喜欢
转载自xjwolaile.iteye.com/blog/1714373
今日推荐
周排行