您的位置:首页 > 编程语言 > Java开发

java基础学习笔记2

2016-01-10 00:18 591 查看
(小白自学,供复习时用,如有不得当之处,烦请大神指教)

视频一直都给我的感觉很不好,一直喜欢看书,找工作急需,目前就先了解大概喽(20160109)

【扣丁学堂】

Test19介绍的是工厂模式,没什么,就是具体的东西抽象化

package practice05;

public class Test19 {
public static void main(String[] args){
/*
* 偶合:使用者直接使用了
*/
//	TCLPhone phone=new TCLPhone();
//	phone.working();
//下面方式降低偶合度
Work work=Factory.getWork("phone");
if(work!=null){
work.working();
}
}
}
//工厂模式
class Factory{
public static Work getWork(String product){
if("phone".equals(product)){
return new TCLPhone();
}else if("tv".equals(product)){
return new TCLTV();
}else{
return null;
}
}
}
interface Work{
public void working();
}
class TCLPhone implements Work{
public void working(){
System.out.println("starting");
}
}
class TCLTV implements Work{
public void working(){
System.out.println("play news");
}
}


Test20好像和Test19讲的都是设计模式,Test20讲的是代理的设计模式,就是代理帮你做一些事情(这些事情你和代理做效果一样),然后你就可以少做一些(必要的)

package practice05;
/**
* 代理设计模式
* @author Administrator
*
*/
public class Test20 {
public static void main(String[] args){
SuperMan man=new SuperMan();
Proxy prox=new Proxy(man);
prox.shopping();
}

}
//主题
interface Subject{
public void shopping();
}
class SuperMan implements Subject{
//	private String name;
public void shopping(){

System.out.println("for wife, I bought commoditys");
//
}
}
//代理
class Proxy implements Subject{
private Subject target;
public Proxy(Subject target){
this.target=target;
}
//实现接口的方法
public void shopping(){
//代购之前要做的事情
System.out.println("priacte");
target.shopping();

//代购后要做的事情
}
}


Test21讲的是适配器模式,以电源器为例,你的手机不能接这个电源,就要用一个转接口来当中介实现,在java中就是用类来继承(implements)

package practice05;
//适配器模式
public class Test21 {
public static void main(String[] args){
PowerA a=new PowerAImpl();
input(a);
PowerB b=new PowerBImpl();
//input(b);//不能直接用,input只能接收a
PowerAdapter adapter=new PowerAdapter(b);
input(adapter);
}
public static void input(PowerA a){
a.connect();
}
}
//适配器
class PowerAdapter implements PowerA{
private PowerB b;
public PowerAdapter(PowerB b){
this.b=b;
}
public void connect(){
b.insert();
}
}
interface PowerB{
public void insert();
}
interface PowerA{
public void connect();
}
class PowerBImpl implements PowerB{
public void insert(){
System.out.println("电源B开始工作");
}
}
class PowerAImpl implements PowerA{
public void connect(){
System.out.println("电源A接口开始工作");
}
}
Test22讲内部类,讲了好几种,都写到下面main里了,不过现在回顾记得是最好定义静态内部类

package practice05;
/*
* 内部类
* 外部类使用方法定义内部类(new内部类)
* 通过外部类.内部类
* 定义内部类时优先考虑今天内部类
* @author Administrator
*
*/
public class Test22 {
public static void main(String[] args){
Outer outer=new Outer();
outer.print();

//直接在外部是用内部类,不推荐
Outer.InnerClass inner=outer.new InnerClass("在外部使用内部类");
inner.print();
//静态类,在外部直接使用静态内部类(创建静态内部类不需要依赖外部类的对象)
//静态内部类在我们之后的android中会经常使用
Outer.StaticInnerClass sic=new Outer.StaticInnerClass();
sic.print();

outer.print3();

outer.print4(new Child(){
public void desc(){
System.out.print("参数内部类");
}
});
}
}
class Outer{
public void print(){
InnerClass inner=new InnerClass("成员内部类");
inner.print();
}
public void print2(){
//在方法中定义内部类
//实例化应该在方法里面
int x=10;//内部类中不能改变x,只能使用,应该定义为final
class InnerClass2{
public void print(){
System.out.println("在方法中定义内部类");
}
}
InnerClass2 inner=new InnerClass2();
inner.print();
}
//内部类才能用static
static class StaticInnerClass{
public void print(){
System.out.println("static inner class");
}
}
class InnerClass{
private String name;
public InnerClass(String name){
this.name=name;
}
public void print(){
System.out.println(name);
}
}
public void print3(){
/*
* 匿名内部类方法
*
*/
Child c=new Child(){
public void desc(){
System.out.println("匿名内部类");
}
};
c.desc();
/*
class InnerClass3 implements Child{

}*/
}
public void print4(Child c){
c.desc();
}
}
interface Child{
public void desc();
}


Test23 链表(很经典),由外部方法进入内部方法并递归

package practice05;
/**
* 很经典的一个例子
* 链表数据结构
* 用于频繁进行添加,插入,删除操作
* @author Administrator
*
*/
public class Test23 {
public static void main(String[] args){
NodeManager mm=new NodeManager();
mm.addNode("1");
mm.addNode("2");
mm.addNode("3");
mm.addNode("4");
mm.addNode("5");
mm.printNode();
mm.delNode("4");
mm.printNode();
}
}

class NodeManager{
private Node root;//根节点

//提供给外部用的三个方法
public void addNode(String name){
if(root==null){
root=new Node(name);
}else{
root.add(name);
}
}
public void delNode(String name){
if(root.getName().equals(name)){
root=root.next;
}else{
root.del(name);
}
}
public void printNode(){
if(root!=null){
System.out.print(root.getName()+"->");
root.print();
System.out.println();
}

}
//每个节点对象
class Node{
private String name;
private Node next;//表示当前节点下一个节点
public Node(String name){
this.name=name;
}
public String getName(){
return name;
}
//添加节点
public void add(String name){
if(this.next==null){
this.next=new Node(name);
}else{
this.next.add(name);//递归
}
}
//删除节点
public void del(String name){
if(this.next!=null){
if(this.next.name.equals(name)){
this.next=this.next.next;
}else{
this.next.del(name);
}
}
}
//打印节点
public void print(){
if(this.next!=null){
System.out.print(this.next.getName()+"->");
this.next.print();
}
}
}
}


Test24 讲java是完全面向对象的,不过我最感兴趣的是赋值-128~127的数时,返回true,而其他的却返回false

package practice05;

public class Test24 {
public static void main(String[] args){
int num=10;
Integer num2=10;//自动装箱
int num3=1+num2;//拆箱
num2.intValue();

//把一个之间内的整数缓存在整数常量池中,这样可以避免不断创建细粒度的对象,造成对内存的消耗
//-128~127,,,下面的语句进行验证
Integer a=200;
Integer b=200;
System.out.println(a==b);
}

}
Test25 异常处理,有点迷糊,对于throw和throws

/**
* try{
* //
* }catch{
*  //
* }
*/
package practice05;
import java.util.*;
public class Test25 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入数字:");
/*		try{
int num3=input.nextInt();
}catch(InputMismatchException e){
System.out.println("输入的类型不匹配");
}
//try语句有可能出现异常
try{
int num1=10;
int num2=0;
System.out.println(num1/num2);
}catch(ArithmeticException e){
System.out.println("算数异常");
}
*/
//try语句有可能出现异常
try{
int num3=input.nextInt();
int num1=10;
int num2=0;
System.out.println(num1/num2);
}
//说下面的异常有等级,因此有先后顺序
catch(ArithmeticException|InputMismatchException e){
System.out.println("异常");
System.out.println(e.getMessage());
}
//			catch( e){
//				System.out.println("输入的类型不匹配");
//			}
finally{
//最后,可以做一些回收,清理的工作
System.out.println("finally语句不管会不会出现异常,都会执行");
}
System.out.println("over");
try {
exTest2(10,2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static int exTest2(int num1,int num2)throws Exception{
//return num1/num2;
if(num2==0)throw new ArithmeticException("ddddd");
return num1/num2;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: