class

Java 设计模式

混江龙づ霸主 提交于 2020-02-26 15:12:06
一 单例模式 : 解决的问题:就是可以保证一个类在内存中的对象唯一性。 public class SingleInstance { private static volatile SingleInstance singleInstance = null; private SingleInstance() { } public SingleInstance getInstance() { if (singleInstance == null) { //A synchronized (SingleInstance.class) { if (singleInstance == null) { singleInstance = new SingleInstance(); //B } } } return singleInstance; } } Note 1:添加volatile修饰避免B处指令重排序时其他线程拿到B处可能还未初始化的单例对象(完成了内存分配,引用的赋值 还未来得及进行对象的初始化)然后在A处判断单例对象不为空直接返回的未初始化的对象。 Note 2: synchronized关键字保证了临界区内变量的可见性和原子性,但无法保证临界区之外访问的变量的原子性。如果对单例对象的读写完全在临界区内,那么就不会出现线程安全问题。不过,这样就带来了性能问题。 Note 3

Python中的元编程

筅森魡賤 提交于 2020-02-26 12:37:56
元编程概念来自lisp和smalltalk 用于生成代码的程序成为元程序metaprogram,编写这种程序就成为元编程metaprogramming Python语言能够通过反射实现元编程。 1 、 type class type(object): def __init__(cls, what, bases=None, dict=None): # known special case of type.__init__ """ type(object_or_name, bases, dict) type(object) -> the object's type type(name, bases, dict) -> a new type # (copied from class doc) """ pass type(name,bases,dict)> a new type返回一一个新的类型。 Type(name,bases,dict)返回一个新的类型; Metaclass=类继承的就是元类了、就是元类的,不写的话直接就是基类。 可以借助type构造任何类,用代码来生成代码,这个就是元编程。 继承列表里面也写metaclass关键字=自已定义的类。 不写的话直接继承type类。 2 、元编程的应用 Attrs是属性的字典。 对照关系: 表、列、行 类、类属性、类实例 ORM

python的metaclass浅析-乾颐堂

爷,独闯天下 提交于 2020-02-26 07:57:40
元类一般用于创建类。在执行类定义时,解释器必须要知道这个类的正确的元类。解释器会先寻找类属性__metaclass__,如果此属性存在,就将这个属性赋值给此类作为它的元类。如果此属性没有定义,它会向上查找父类中的__metaclass__.如果还没有发现__metaclass__属性,解释器会检查名字为__metaclass__的全局变量,如果它存在,就使用它作为元类。否则, 这个类就是一个传统类,并用 types.ClassType 作为此类的元类。 在执行类定义的时候,将检查此类正确的(一般是默认的)元类,元类(通常)传递三个参数(到构造器): 类名,从基类继承数据的元组,和(类的)属性字典。 元类何时被创建? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #!/usr/bin/env python print '1. Metaclass declaration' class Meta( type ): def __init__( cls , name, bases, attrd): super (Meta, cls ).__init__(name,bases,attrd) print '3. Create class %r' % (name) print '2. Class Foo

前端学习之jquery

喜欢而已 提交于 2020-02-26 05:08:43
一 jQuery是什么? <1> jQuery 由美国人 John Resig 创建,至今已吸引了来自世界各地的众多 javascript 高手加入其 team 。 <2>jQuery 是继 prototype 之后又一个优秀的 Javascript 框架。其宗旨是 ——WRITE LESS,DO MORE ! <3>它是轻量级的 js 库 ( 压缩后只有 21k) ,这是其它的 js 库所不及的,它兼容 CSS3 ,还兼容各种浏览器 ( IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+ )。 <4>jQuery 是一个快速的,简洁的 javaScript 库,使用户能更方便地处理 HTML documents 、 events 、实现 动画效果,并且方便地为网站提供 AJAX 交互。 <5>jQuery 还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件 可供选择。 二 什么是 jQuery 对象? jQuery 对象就是通过 jQuery 包装 DOM 对象后产生的对象。 jQuery 对象是 jQuery 独有的 . 如果一个对象是 jQuery 对象 , 那么它就可以使用 jQuery 里的方法 : $(“#test”).html(); $("#test").html() //意思是指

jQuery代码节选(筛选)

人盡茶涼 提交于 2020-02-26 05:07:28
筛选 ... 8.not() <p class="p1">1</p> <p class="p2">2</p> <p class="p3">3</p> console.log($("p").not($('.p1'))); 从获取的p元素集合中除去拥有 class='p1' 9.slice() <p class="p1">1</p> <p class="p2">2</p> <p class="p3">3</p> console.log($("p").slice(0,1)); console.log($("p").slice(0,-2)); 10.children() <div> <p class="p1">1</p> </div> <div> <p class="p2">2</p> </div> <p class="p3">3</p> console.log($("div").children('.p1')); console.log($("div").children()); 11.closest() <div class="father"> <div class="children"> 我是div <p >我是孙子p</p> </div> <p>我是儿子p</p> </div> <p>我是兄弟p</p> console.log($(".father p").closest("div

观察者模式

依然范特西╮ 提交于 2020-02-26 04:53:02
package com.thread.ch4; import java.util.ArrayList; import java.util.List; /** * 观察者模式 */ public class Subject { //被观察的对象 private List<Observer> observerList = new ArrayList<>(); private int state; public int getState() { return state; } public void setState(int state) { if (state == this.state) { return; } this.state = state; notifyAllObserver(); } public void attach(Observer observer) { observerList.add(observer); } public void notifyAllObserver() { observerList.stream().forEach(Observer::update); } } package com.thread.ch4; public abstract class Observer { protected Subject subject; public

overloading operators issue with addition

邮差的信 提交于 2020-02-26 04:16:27
问题 Having issues with adding objects. It seems to work when adding two, ex: 2.34 + 34.57 = 36.81, but fails when adding 3 or more ex: 6.78 + 9.81 + 4.59 = 79.59 <-- the total seems to increase drastically for some reason. The converter functions can be ignored since they simply translate the numerical total to english format and are working. The median and sorting, and > functions work as well, so the issue might be somewhere in the ostream, istream, or + functions. I used cout a whole bunch of

overloading operators issue with addition

耗尽温柔 提交于 2020-02-26 04:16:05
问题 Having issues with adding objects. It seems to work when adding two, ex: 2.34 + 34.57 = 36.81, but fails when adding 3 or more ex: 6.78 + 9.81 + 4.59 = 79.59 <-- the total seems to increase drastically for some reason. The converter functions can be ignored since they simply translate the numerical total to english format and are working. The median and sorting, and > functions work as well, so the issue might be somewhere in the ostream, istream, or + functions. I used cout a whole bunch of

jquery前缩后缩分页和从后台获取数据代码

坚强是说给别人听的谎言 提交于 2020-02-26 02:54:13
1.分页 前缩后缩情况 function createPage(currentpage, totlepage) { //创建20个页码 //根据当前页 实现 后缩 前后缩 前缩 var str = ""; str += "<li class='pageup'>上一页</li>" //1...1617181920 当前页在后边显示 //1 2 3 4 5....20 当前页在前边显示 for (var i = 1; i <= totlepage; i++) { if (i == 2 && currentpage - 3 > i) { //前缩 i = currentpage - 3; str += "<li class='pageli'>...</li>"; } else if (i == currentpage + 3 && currentpage + 3 < totlepage) { //后缩 i = totlepage - 1; str += "<li class='pageli'>...</li>"; } else { if (i == currentpage) { str += "<li class='pageli ck'>" + i + "</li>" } else { str += "<li class='pageli'>" + i + "</li>" } } }

Does the new SvgImageSource class designed for UWP can be used in WPF project

守給你的承諾、 提交于 2020-02-26 01:00:04
问题 It seems that a relatively new class: SvgImageSource can only be used in UWP. Is there any way to use it in a WPF project and how? 回答1: It will be possible to use any UWP XAML UI in WPF using XAML Islands. This is currently available as a preview using the WindowsXamlHost control which is part of the Windows Community Toolkit . Also note, that when using UWP controls in WPF apps, the app will then work only on Windows 10 client PCs. 回答2: Please take a look at this framework: SharpVectors The