我正在将一些地理定位java代码从http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates#Java(如下所示)移植到python.它可以使用两个函数(fromDegrees或fromRadians)进行初始化.我以为我可以做点什么
class geoLocation:
_radLat = 0
_radLong = 0
_degLat = 0
_degLong = 0
def fromDegrees(lat, long):
#set _radLat, _radLong, _degLat, _degLong
def fromRadians(lat, long):
#set _radLat, _radLong, _degLat, _degLong
...
Run Code Online (Sandbox Code Playgroud)
但这似乎不是最佳的,因为我设置了两次_radLat,_radLong,_degLat和_degLong的值.我可以定义两个init函数吗?最好的方法是什么?
谢谢
/**
* <p>Represents a point on the surface of a sphere. (The Earth is almost
* spherical.)</p>
*
* <p>To create an instance, call one of the static methods fromDegrees() or
* fromRadians().</p>
*
* <p>This code was originally published at
* <a href="http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates#Java"> …Run Code Online (Sandbox Code Playgroud) 如果我们不想在我们的类中实现init方法,并且记住NSObject中的init只返回对象的实例而没有初始化,如果我们已经使用alloc获取实例,我看不到调用init的意义.我已经尝试过并且有效,但我不确定它是否会导致未来出现问题.
myClass *newObject = [myClass alloc];
Run Code Online (Sandbox Code Playgroud)
代替:
myClass *newObject = [[myClass alloc] init];
Run Code Online (Sandbox Code Playgroud)
非常感谢.
我最近开始使用Python 2.6 for Ubuntu Server admin,并且有两个关于冗余的小问题:
首先是进口:它们看起来都像
从类导入类
from class import Class
Run Code Online (Sandbox Code Playgroud)
第二件事是__init__方法:
__init__(self,arg1,...,argn):
self.arg1 = arg1
...
self.argn = argn
Run Code Online (Sandbox Code Playgroud)
有没有办法避免这些重复?
在CustomCell.m中,我定义了init方法,我想从IB加载单元格:
- (id)init {
self = [super init];
if (self) {
NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
self = [nib objectAtIndex:0];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
在方法cellForRowAtIndexPath中的MyTableViewController.m中,我初始化我的自定义单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell=[[CustomCell alloc]init];
return cell;
Run Code Online (Sandbox Code Playgroud)
}
一切都按照我的预期运作,但当我做的时候,Product -> Analyse我得到了
Returning 'self' while it is not set to the result of '[(super or self) init...]'
我做错了什么?
我想实现自己的矩阵类,它继承自numpy的矩阵类.
numpy的矩阵构造函数需要一个属性,比如("1 2; 3 4'").相反,我的构造函数不需要任何属性,应该为超级构造函数设置一个默认属性.
这就是我做的:
import numpy as np
class MyMatrix(np.matrix):
def __init__(self):
super(MyMatrix, self).__init__("1 2; 3 4")
if __name__ == "__main__":
matrix = MyMatrix()
Run Code Online (Sandbox Code Playgroud)
因为我不断收到此错误,所以此代码中必定存在一个愚蠢的错误:
this_matrix = np.matrix()
TypeError: __new__() takes at least 2 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)
我真的对此毫无头绪,谷歌搜索到目前为止没有帮助.
谢谢!
我很难弄清楚我遇到的一些代码的目的.
代码有一个类Foo,它有一个__init__带多个参数的方法.从我到目前为止学到的Python,通过调用Foo('bar'),它将传递这个字符串作为参数__init__(我认为它应该相当于一个构造函数).
但我遇到的问题是我正在查看的代码是Foo.__init__('bar')直接调用.这样做的目的是什么?我几乎觉得我错过了其他目的__init__.
是否有可能从Xcode自动生成自定义init方法,因为Android Studio适用于Android?我的意思是,如果我在.h中声明一些属性,例如:int a; int b;
所以,我想自动创建一个init方法:
- (id)initWithA:(int) aInner andB:(int) bInner
{
a = aInner;
b = bInner;
}
Run Code Online (Sandbox Code Playgroud) 我收到错误
无法在当前上下文中推断闭包类型
在使用Swift 1.2的代码中
private lazy var _messagesVC = { return MessagesViewController(nibName:"MessagesViewController",bundle:nil)}()
Run Code Online (Sandbox Code Playgroud)
整个视图控制器,我收到此错误
import UIKit
class FriendsViewController: UIViewController {
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var segmentContainerView: UIView!
private lazy var _connectionVC = { return FriendsConnectionViewController(nibName:"FriendsConnectionViewController",bundle:nil)}()
private lazy var _messagesVC = { return MessagesViewController(nibName:"MessagesViewController",bundle:nil)}()
override func viewDidLoad() {
super.viewDidLoad()
self.selectedControllerFrom(index: 0)
// Do any additional setup after loading the view.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
func selectedControllerFrom(index index:UInt)
{
var vc:UIViewController?
switch index{
case 0: vc …Run Code Online (Sandbox Code Playgroud) 在搜索之后,我仍然感到困惑,你是否可以让一个运行Ubuntu 的docker容器与一个工作的init系统(upstart)和syslog一起运行.
我知道docker容器用于运行单个进程而不是完整的操作系统,但我的用例是测试各种Linux发行版上的守护进程,确保守护进程在崩溃时成功启动,停止和重新启动等,并记录到syslog .所以我试图决定是否可以使用docker容器,或者我可能会更好地使用Vagrant.
我发现的一些资源令人困惑:
容器无法连接到Upstart docker/docker#1024
因为Docker用它自己替换了默认的/ sbin/init,所以无法在Docker容器中运行Upstart init.
传统上,Docker容器在启动时会运行单个进程,例如Apache守护程序或SSH服务器守护程序.通常,您希望在容器中运行多个进程.您可以通过多种方式实现此目标,从使用简单的Bash脚本作为容器
CMD指令的值到安装流程管理工具.
基本上我最终需要的是能够运行:
$ initctl start <daemon>
$ initctl stop <daemon>
Run Code Online (Sandbox Code Playgroud)
当然,在创建必要的conf文件后/etc/init/<daemon>.conf,查看日志syslog.
我想用alpine和apache创建一个docker图像.我使用tini作为"init"系统.它工作,直到我分离并重新连接到容器.连接到容器后,apache退出并且容器停止.我不知道问题是什么.有没有人与docker,alpine和apache有类似的问题?
我的Dockerfile看起来像这样(之前,我使用Alpines包管理器进行tini)
FROM alpine
ENV TINI_VERSION v0.16.1
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-static /sbin/tini
RUN chmod +x /sbin/tini
RUN apk add --no-cache apache2 \
&& mkdir -p /run/apache2 \
&& ln -sf /dev/stdout /var/log/apache2/access.log \
&& ln -sf /dev/stderr /var/log/apache2/error.log
EXPOSE 80
ENTRYPOINT ["/sbin/tini", "-vvv", "-g", "--"]
CMD ["/usr/sbin/httpd", "-f", "/etc/apache2/httpd.conf", "-DFOREGROUND"]
Run Code Online (Sandbox Code Playgroud)
输入和输出到docker cli:
~/Desktop/docker_test@laptop-sebi
$ docker run -itd test1
a793bad5d4350f58893909f1552c9f2978d8e2952960ac667f8dcb2bf7a3516e
~/Desktop/docker_test@laptop-sebi
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
a793bad5d435 test1 "/sbin/tini -vvv -..." 12 seconds
ago …Run Code Online (Sandbox Code Playgroud) init ×10
python ×4
docker ×2
ios ×2
iphone ×2
xcode ×2
apache ×1
cocoa-touch ×1
constructor ×1
dockerfile ×1
import ×1
inheritance ×1
numpy ×1
objective-c ×1
redundancy ×1
swift ×1
syslog ×1
types ×1
ubuntu ×1
uitableview ×1
upstart ×1