`

11月5号学习(第3天)

 
阅读更多
计划
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
分享到:
评论

相关推荐

    计算机应用基础中职PPT学习教案.pptx

    3 第一章 计算机基础知识 计算机的发展 1 计算机的组成 2 常用计算机设备 3 计算机病毒的防治 4 习题 5 第2页/共11页 计算机应用基础中职PPT学习教案全文共11页,当前为第3页。 4 1.1 计算机的定义 计算机又称"电脑...

    Java入门1·2·3:一个老鸟的Java学习心得.PART3(共3个)

    第3章 Java中的基本数据类型和运算符 33 教学视频:1小时5分钟 3.1 Java中的基本数据类型 33 3.1.1 基本数据类型——编程语言中的数据原子 33 3.1.2 Java中的基本上数据类型介绍 34 3.1.3 基本数据类型值域 34 ...

    第三届操作技能大赛平面设计大赛方案.doc

    第三届操作技能大赛平面设计大赛方案 为了提高实训处学生的动手实践能力和创新能力,提高学生学习photoshop的兴趣,活 跃校园学习气氛,培养学生的职业技能,为就业打下基础。 一、大赛的指导思想 1、充分展示麻城理工...

    【理论学习班】锁相环设计、仿真与应用(第5版)中文版 PART2

    因为频率综合器是DPLL数字锁相环最重要的应用之一,所以单立第3章深入讨论数字锁相环频率综合器。因为相位抖动和寄生边带是频率综合器最烦人的现象,我们给出了不同的解决这些问题的方法,即抗齿隙式电路和高阶环路...

    【理论学习班】锁相环设计、仿真与应用(第5版)中文版 PART1

    因为频率综合器是DPLL数字锁相环最重要的应用之一,所以单立第3章深入讨论数字锁相环频率综合器。因为相位抖动和寄生边带是频率综合器最烦人的现象,我们给出了不同的解决这些问题的方法,即抗齿隙式电路和高阶环路...

    Java程序设计习题集下载

    从2000年3月到2000年6月在香港科技大学计算机系担任访问研究员,从2000年11月到2002年9月在美国肯塔基大学计算机系做博士后,现为清华大学软件学院教师。参与或负责多个国家自然科学基金项目。国家863项目。973项目...

    python入门到高级全栈工程师培训 第3期 附课件代码

    第3章 01 网络基础和dos命令 02 为何学习linux 03 课程内容介绍 04 操作系统内核与系统调用 05 操作系统安装原理 06 linux操作系统安装 07 初识linux命令 08 linux操作系统目录结构 09 目录及文件操作 第4章 01 ...

    c#学习笔记.txt

    如前所述,我是一个狮子座男人,一度我认为学习Java会使我看起来与众不同,可是几个月以后我放弃了这个选择,我看了论坛里关于这两种语言孰优孰劣的讨论,最终选择了C#,请不要问我为何做出这样的选择,很多人认为...

    JAVA入门1.2.3:一个老鸟的JAVA学习心得 PART1(共3个)

    第3章 Java中的基本数据类型和运算符 33 教学视频:1小时5分钟 3.1 Java中的基本数据类型 33 3.1.1 基本数据类型——编程语言中的数据原子 33 3.1.2 Java中的基本上数据类型介绍 34 3.1.3 基本数据类型值域 34 ...

    第三届操作技能大赛平面设计大赛方案.pdf

    第三届操作技能大赛平面设计大赛方案 为了提高实训处学生的动手实践能力和创新能力,提高学生学习 photoshop的兴趣,活跃校园学习气氛,培养学生的职业技能,为就业打 下基础。 一、大赛的指导思想 1、充分展示麻城...

    java基础笔记

    第三节 7月20日 7 一、条件语句 7 二、循环语句 8 三、 几个循环的例子 9 第四节 7月21日 10 一、 方法的定义及声明 10 二、 方法的调用 10 三、 方法的重载 11 四、 基本数据类型对应的封装类 11 五、 几个常用的...

    java 经典习题.doc

    题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21.... ...

    入门学习Linux常用必会60个命令实例详解doc/txt

    在第三种格式中,会创建所有指定的目录及它们的主目录。长选项必须用的参数在使用短选项时也是必须的。 3.主要参数 --backup[=CONTROL]:为每个已存在的目的地文件进行备份。 -b:类似 --backup,但不接受...

    Linux实验报告-实验一.docx

    中北大学软件学院 实验报告 专 业: 软件工程 方 向: 软件开发与测试 课程名称: Linux程序设计 班 级: 学 号: 姓 名: 辅导教师: 唐道光 2017年4月制 Linux实验报告-实验一全文共11页,当前为第2页。...

    计算机应用基础(第二版)--3-2.pptx

    计算机应用基础(第二版)--3-2全文共50页,当前为第3页。 文档段落、首字下沉、边框和底纹设置 2 插入图片和艺术字 3 文档字符格式的设置 1 Word 2010文档排版 计算机应用基础(第二版)--3-2全文共50页,当前为第4页...

    人工智能AlphaGo.pptx

    AlphaGo的成长之路 人工智能AlphaGo全文共40页,当前为第3页。 这是什么狗? AlphaGo的成长之路 人工智能AlphaGo全文共40页,当前为第4页。 阿尔法围棋(AlphaGo)是第一个击败人类职业围棋选手、第一个战胜围棋...

    2009达内SQL学习笔记

    3、对列起别名:有直接起别名,加AS起别名,用双引号起别名等三种方法 (单引号,引起字符串;双引号,引起别名。起别名有符号,或者区分大小写时,必须用双引号) 多表查询时,可给表起别名。(给列起别名,列&lt;空格...

    Google Android SDK开发范例大全(第3版) 3/5

    第3版 (2011年11月1日) 丛书名: 移动开发系列丛书 平装: 818页 正文语种: 简体中文 开本: 16 ISBN: 9787115264305 条形码: 9787115264305 商品尺寸: 26 x 18.4 x 3.8 cm 商品重量: 1.3 Kg 编辑本段 内容简介 ...

    无线网络优化实习报告.doc

    2 2.3进度安排 2 2.4实验条件 2 3理论学习 3 3.1第一周学习内容规划 3 3.2第二周学习内容规划 3 4现场测试 4 4.1测试方法 4 4.2 测试结果与分析 5 4.2.1 PCI统计 5 4.2.2 RSPR测试 7 4.2.3 SINR测试 9 4.2.4下载速率...

Global site tag (gtag.js) - Google Analytics