static

Java中static的作用总结

独自空忆成欢 提交于 2020-01-16 07:49:10
1、深度总结    引用一位网友的话,说的非常好,如果别人问你static的作用;如果你说静态修饰 类的属性 和 类的方法 别人认为你是合格的;如果是说 可以构成 静态代码块,那别人认为你还可以; 如果你说可以构成 静态内部类, 那别人认为你不错;如果你说了静态导包, 那别人认为你很OK;   那我们就先在这几方面一一对static进行总结;然后说一些模糊的地方,以及一些面试中容易问道的地方; 1)static方法   static方法一般称作静态方法,由于静态方法不依赖于任何对象就可以进行访问,因此对于静态方法来说,是没有this的,因为它不依附于任何对象,既然都没有对象,就谈不上this了。并且由于这个特性,在静态方法中不能访问类的非静态成员变量和非静态成员方法,因为非静态成员方法/变量都是必须依赖具体的对象才能够被调用。   但是要注意的是,虽然在静态方法中不能访问非静态成员方法和非静态成员变量,但是在非静态成员方法中是可以访问静态成员方法/变量的。举个简单的例子:   在上面的代码中,由于print2方法是独立于对象存在的,可以直接用过类名调用。假如说可以在静态方法中访问非静态方法/变量的话,那么如果在main方法中有下面一条语句:   MyObject.print2();   此时对象都没有,str2根本就不存在,所以就会产生矛盾了。同样对于方法也是一样

Simulate static variables in python with closures

这一生的挚爱 提交于 2020-01-16 01:21:06
问题 Is it possible to write a function in python, which takes an argument a and prints the result of h+a where h is a local variable. Then it should return itself, with h increased by one. 回答1: Yes, you can def f(a): def inner(h, a): print h+a return lambda (x): inner(h+1, x) return inner(1, a) example g = f(0) # +1 g = g(0) # +2 g = g(0) # +3 f(0) # +1 g(0) # +4 g(0) # +4 prints 1 2 3 1 4 4 Q.E.D. 回答2: In python 3, you can do this: >>> def f(a): ... h = 1 ... def inner(): ... nonlocal h ...

Java 修饰符

强颜欢笑 提交于 2020-01-16 01:19:22
Java 修饰符 Java语言提供了很多修饰符,主要分为以下两类: 访问修饰符 非访问修饰符 修饰符用来定义类、方法或者变量,通常放在语句的最前端。我们通过下面的例子来说明: public class className { // ... } private boolean myFlag; static final double weeks = 9.5; protected static final int BOXWIDTH = 42; public static void main(String[] arguments) { // 方法体 } 访问控制修饰符 Java中,可以使用访问控制符来保护对类、变量、方法和构造方法的访问。Java支持4种不同的访问权限。 默认的,也称为default,在同一包内可见,不使用任何修饰符。 私有的,以private修饰符指定,在同一类内可见。 共有的,以public修饰符指定,对所有类可见。 受保护的,以protected修饰符指定,对同一包内的类和所有子类可见。 默认访问修饰符-不使用任何关键字 使用默认访问修饰符声明的变量和方法,对同一个包内的类是可见的。接口里的变量都隐式声明为public static final,而接口里的方法默认情况下访问权限为public。 实例: 如下例所示,变量和方法的声明可以不使用任何修饰符。 String

Would const values be stored per instance?

匆匆过客 提交于 2020-01-15 15:57:12
问题 For a type like this: public class BlurEffect { public const string Name = "Blur"; public int Amount {get;set;} } I have a few members like .Name , that will be the same across all members, should I make it static instead? I just don't want it to be stored per instance, or use more resources than necessary, and they should also be read-only. Lastly I want instance access for it so I can say: blurEffect.Name Should I make a Name property that returns a private static variable? Is this the best

Java——修饰符详解

╄→гoц情女王★ 提交于 2020-01-15 11:38:55
Java 修饰符 Java语言提供了很多修饰符,主要分为以下两类: 访问修饰符 非访问修饰符 修饰符用来定义类、方法或者变量,通常放在语句的最前端。我们通过下面的例子来说明: 实例: public class className { // ... } private boolean myFlag ; static final double weeks = 9.5 ; protected static final int BOXWIDTH = 42 ; public static void main ( String [ ] arguments ) { // 方法体 } 访问控制修饰符 Java中,可以使用访问控制符来保护对类、变量、方法和构造方法的访问。Java支持4种不同的访问权限。 默认的,也称为 default,在同一包内可见,不使用任何修饰符。 私有的,以 private 修饰符指定,在同一类内可见。 共有的,以 public 修饰符指定,对所有类可见。 受保护的,以 protected 修饰符指定,对同一包内的类和所有子类可见。 我们可以可以通过以下表来说明访问权限: 修饰符 当前类 同一包内 子孙类 其他包 public Y Y Y Y protected Y Y Y N default Y Y N N private Y N N N 默认访问修饰符-不使用任何关键字

JUnit initialization of static fields

帅比萌擦擦* 提交于 2020-01-15 11:22:53
问题 I'm using JUnit for unit testing. Let's say I want to test class B (methods of class B ). Let's say we have another class A which is the main class (contains main method) and has some protected static fields. Now, it is the case that class B uses some of these static fields of class A . So if I'm testing class B these static fields of class A does not exist. How can I test class B without executing the program (executing class A )? Edit: I have to clarify it. Let's assume we have the

do extended classes inherit static var values (PHP)?

坚强是说给别人听的谎言 提交于 2020-01-15 11:19:06
问题 If I have a base class that contains a static var, I then set this static var, and then have a class that extends the base class, will the extended class retain the value of the static var that I have already set in the base class? 回答1: Yes, although they're different variables, the static variables in both classes are in the same reference set. You can break this reference set though, by using reference assignment ( =& ) or by redeclaring it in the extended class: class base { public static

Django 静态文件

三世轮回 提交于 2020-01-15 10:17:42
静态文件 什么是静态文件 静态文件是网站页面所使用到的提前已经写好的文件,如css,js,第三方组件 bootstrap,sweetalert,fontawesome等 网站所使用到的html文件统一放到templates文件夹中 那针对网站所使用到的静态文件也应该单独放到一个文件夹中存储,这个文件夹默认情况下都叫static,该文件夹需要自己手动创建 #该文件夹内部通常是以下结构static -css 网站所用到的所有的css文件 -js 网站所用到的所有的js文件 -image 网站所用到的所有的图片文件 第三方文件 Django静态文件配置 django在配置文件中给你暴露了配置文件的配置信息,你只需要按照固定的写法书写即可暴露对应的静态文件资源 如何实现动态绑定 {% load static %} <link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}"> <script src="{% static 'bootstrap-3.3.7-dist/js/bootstrap.min.js' %}"></script> 来源: https://www.cnblogs.com/baohanblog/p/12195101.html

Django cannot find my static files

拜拜、爱过 提交于 2020-01-15 10:12:08
问题 I am relatively new to web dev. and I am trying to build my first web application. I have my static folder in project_root/static but for some reason, I keep getting 404s when I run the server: Not Found: /users/login/js/bootstrap.min.js for example. I have {% load staticfiles %} at the top of my html and in my settings.py I have: BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') Thanks. EDIT: Fixed

Static Methods or Not? Global variables?

為{幸葍}努か 提交于 2020-01-15 09:32:28
问题 I want to know what way is more efficient. No global variables, passing variables through parameters, having all methods static No global variables, having only main method static and creating class object in main to access methods Use only global variables, having only main method static and creating class object in main to access methods I am currently using method 3 but I want to know what is more efficient. This class will not be used by any other class outside of it, it pretty much