我有这个界面:
public interface Animal {
public void Eat(String name);
}
Run Code Online (Sandbox Code Playgroud)
这段代码实现了这个接口:
public class Dog implements Animal {
public void Eat(String food_name) {
System.out.printf(food_name);
}
public static void main(String args[]) {
Animal baby2 = new Dog(); //HERE!!!!!!!!!!!!!!!!!!!!!!
baby2.Eat("Meat");
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么代码有效?无法实例化接口.然而在这种情况下,界面被实例化(标记为"HERE !!!!!!!!!!!!!").
这里发生了什么?
我到处搜索,但他们的解决方案需要某种形式的IP地址.以下是我找到的解决方案.
require 'socket'
#METHOD 1
ip = IPSocket.getaddress(Socket.gethostname)
puts ip
#METHOD 2
host = Socket.gethostname
puts host
#METHOD 3(uses Google's address)
ip = UDPSocket.open {|s| s.connect("64.233.187.99", 1); s.addr.last}
puts ip
#METHOD 4(uses gateway address)
def local_ip
orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily
UDPSocket.open do |s|
s.connect '192.168.1.1', 1
s.addr.last
end
ensure
Socket.do_not_reverse_lookup = orig
end
ip=local_ip
puts ip
Run Code Online (Sandbox Code Playgroud)
所有这些都需要某人的IP地址.有没有使用别人的IP地址的解决方案?优选地,平台无关.
这是代码.我只是想测试System.ServiceProcess库的库.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceProcess;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hi");
var srv = new ServiceController("MyService");
Console.WriteLine("MyService Status {0}", srv.Status);
if (srv.Status != ServiceControllerStatus.Running)
srv.Start();
System.Threading.Thread.Sleep(1000000);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行C#代码时,它说:
错误1命名空间"System"中不存在类型或命名空间名称"ServiceProcess"(您是否缺少程序集引用?)
什么地方出了错?
这是xml文件."CategoryName"元素中的空格和回车符是有意的.
<?xml version="1.0" encoding="utf-8"?>
<group>
<item>
<id>item 1</id>
<CategoryName>
</CategoryName>
</item>
<item>
<id>item 2</id>
<CategoryName></CategoryName>
</item>
<item>
<id>item 3</id>
<CategoryName> </CategoryName>
</item>
</group>
Run Code Online (Sandbox Code Playgroud)
以下是上述XML文件的XSLT文件.它应该做的是它将清除"CategoryName"元素中的所有空格.然后,它将测试"CategoryName"是否为空.
<?xml version="1.0" encoding="utf-8"?>
<!-- DWXMLSource="testempty.xml" -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:strip-space elements="*" /> <!--HERE IS STRIP SPACE-->
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Untitled Document</title>
</head>
<body>
<xsl:for-each select="/group/item">
<xsl:if test="CategoryName = ''"> <!--HERE IS THE TEST-->
<p>Empty</p> <!--IT WILL OUTPUT 'EMPTY' IF THE ELEMENT IS …Run Code Online (Sandbox Code Playgroud) 我有这个代码
`ifconfig`
%x(ifconfig)
system("ifconfig")
Run Code Online (Sandbox Code Playgroud)
当我运行它时,只有"system("ifconfig")"工作.我知道,因为我先分开运行它们.
我用Google搜索了很长时间,但我仍然不明白它是如何工作的,因为大多数解释都是非常技术性的,没有任何插图可以让它更清晰.我的主要困惑是它与虚拟内存有什么不同?
我希望这个问题在这里有一个非常好的解释,以便其他提出相同问题的人可以在谷歌时找到它.
我有以下 Ruby 代码:
cmd="
source= $(mktemp)
echo source
"
system("#{cmd}")
system("source= $(mktemp)")
Run Code Online (Sandbox Code Playgroud)
我希望代码执行“mktemp”命令并将临时文件名输出到变量“source”。但是,我收到的错误消息是:
sh: /tmp/tmp.EpXeLNkqjN: Permission denied
sh: /tmp/tmp.wVCqdqHSpp: Permission denied
------------------
(program exited with code: 0)
Press return to continue
Run Code Online (Sandbox Code Playgroud)
即使我以 root 身份运行该程序,错误也是一样的。
但是,当我仅运行 mktemp 命令时,没有问题。怎么了?