zoukankan      html  css  js  c++  java
  • 从入门到放弃的第二周(面向对象)......day.8.。。。。。单例模式,重载,封装,包装;

     

    1,设计模式(Design Pattern)之单例模式(Singleton)

     

    作用:保证一个系统(单JVM)中只有一个实例
    实现方式:首先确保构造方法使用private修饰

     


    1,懒汉式:在第一次获取实例是才实例化,注意线程的安全性
    public class Singleton {
    private Singleton() {
    }

    private static Singleton singleton;

    public static Singleton getInstance() {
    if (singleton == null) {
    singleton = new Singleton();
    }
    return singleton;
    }
    }

     

     


    2,饿汉式:第一次使用该类时,就会实例化,线程安全,但是性能稍差

     


    public class Singleton {
    private Singleton() {
    }

    public static int num=0;//即使访问num,其他的静态属性(如singleton)也会被初始化

    private static Singleton singleton = new Singleton();

    public static Singleton getInstance() {
    return singleton;
    }
    }

    2,封装(Encapsulation)

     


    将实现的细节封装起来,提供一些公共的接口供外部访问
    作用:安全,简化调用
    实现:使用访问控制修饰符

     

    3,包装类

     

    1. int Integer
    2. long Long
    3. short Short
    4. float Float
    5. double Double
    6. byte Byte
    7. char Character
    8. boolean Boolean

     

     

    思路;类的作用或功能,如何实例化,初始化,
    提供的方法:作用,参数,返回值
    Integer:对int包装,


    方法:int num2=Integer.parseInt("123"); 将字符串转换为整数

     

     

    自动装箱与自动拆箱 JDK1.5 支持 J2EE J2SE Jakarta EE
    自动装箱:基本数据类型自动转换为对应的包装类
    自动拆箱:包装类自动转换为对应的基本数据类型

     

     

    任务:判断某个字符是不是数字?
    char a='a';
    char b='1'; 找一个包装类,调用其中某个方法

     

     

    面试题:
    Integer num1=100;
    Integer num2=100;
    System.out.print(num1==num2);

     

    变量的值在-128-127之间,,结论是true,否则是false

     

     

    4,方法重载(overload)

     


    在一个类中,方法的名称一样,但是参数列表不同
    在方法的形式参数是基本类型时,按照基本类型 的范围依次调用方法,如果没有基本类型,则调用参数为对应包装类的方法,如果没有对应的包装类型,则调用包装类的父类型作为参数的方法

     

     

    5,this

     


    代表当前的实例(instance)
    this不能用于静态作用域(方法,代码块)

     

    使用场景:
    1,用于实例方法,可以访问到该实例的属性或方法 this.属性/方法
    2,用于构造方法,可以访问该类的其他重载的构造方法 this(参数) 必须出现在第一行

     


    1,声明一个类(Point),代表二维坐标上的一个点,具有属性x,y,分别表示横坐标和纵坐标
    要求使用构造方法对其属性赋值:一种是无参的,默认将x,y赋值为0
    第二种,接收两个参数,分别为x,y赋值
    2,声明一个操作Point的类,声明方法:
    2.1 计算一个点离原点的距离 Math.sqrt
    2.2 计算任意两个点的距离
    public double getInstant(Point p1,Point p2){

    }

    2.3 判断两个点是否在一条水平线上
    2.4 判断三个点是否在一条直线上
    3,编写测试类

    public class Point {
    	public Point() {
    		this.x = 0;
    		this.y = 0;
    	}
    	public Point(double num1, double num2) {
    		this.x = num1;
    		this.y = num2;
    	}
    	public double getX() {
    		return x;
    	}
    	public void setX(double x) {
    		this.x = x;
    	}
    	public double getY() {
    		return y;
    	}
    	public void setY(double y) {
    		this.y = y;
    	}
    	private double x;
    	private double y;
    }
    

      

     

    public class Pointmaker {
    
    	public void pt(Point p) {
    		double juli = (p.getX() * p.getX() + p.getY() * p.getY());
    		double x = Math.sqrt(juli);
    		System.out.print(x);
    	}
    	public void pt(Point p, Point q) {
    		double juli = (p.getX() - q.getX()) * (p.getX() - q.getX()) + (p.getY() - q.getY()) * (p.getY() - q.getY());
    		double x = Math.sqrt(juli);
    		System.out.print(x);
    	}
    	public void pts(Point p, Point q) {
    		if (p.getY() == q.getY()) {
    			System.out.println("yes");
    		} else {
    			System.out.println("no");
    		}
    	}
    	public void pt(Point p, Point q, Point z) {
    		if ((p.getY() / p.getX()) == (q.getY() / q.getX()) && (p.getY() / p.getX()) == (z.getY() / z.getX())) {
    			System.out.println("yes");
    		} else {
    			System.out.println("no");
    		}
    	}
    }
    

      

     

    public class test {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Pointmaker pointtest = new Pointmaker();
    		Point point1 = new Point(3, 4);
    		Point point2 = new Point(6, 8);
    		Point point3 = new Point(9, 12);
    
    		pointtest.pt(point1);
    		pointtest.pt(point1, point2);
    		pointtest.pt(point1, point2, point3);
    		pointtest.pts(point1, point2);
    		double x = Math.pow(2, 2);
    		System.out.print(x);
    	}
    }
    

      

                       这一章除了单例模式和面试的那个范围有点绕,其他的都不算很难。

                                                                

     

  • 相关阅读:
    陶瓷电容的结构、工艺、失效模式
    Vue.js最佳实践
    Vue 超快速学习
    CSS 小技巧
    HTML5 Canvas
    webkit下面的CSS设置滚动条
    Some untracked working tree files would be overwritten by checkout. Please move or remove them before you can checkout. View them
    JSCS: Please specify path to 'JSCS' package
    React中ref的使用方法
    React 60S倒计时
  • 原文地址:https://www.cnblogs.com/suxiao666/p/11342785.html
Copyright © 2011-2022 走看看