代码001

public class EqualsTest {
public static void main (String [] args) {
Moof one = new Moof(8);
Moof two = new Moof(8);
if (one.equals(two)) {
System.out.println("one and two are equal");
}
}
}
class Moof {
private int moofValue;
Moof(int val) {
moofValue = val;
}
public int getMoofValue() {
return moofValue;
}
public boolean equals(Object o) {
if ((o instanceof Moof) && (((Moof)o).getMoofValue()
== this.moofValue)) {
return true;
} else {
return false;
}
}
}
The equals() Contract
Pulled straight from the Java docs, the equals() contract says
■ It is reflexive. For any reference value x, x.equals(x) should return true.
■ It is symmetric. For any reference values x and y, x.equals(y) should
return true if and only if y.equals(x) returns true.
■ It is transitive. For any reference values x, y, and z, if x.equals(y) returns
true and y.equals(z) returns true, then x.equals(z) must return true.
■ It is consistent. For any reference values x and y, multiple invocations of
x.equals(y) consistently return true or consistently return false, provided
no information used in equals comparisons on the object is modified.
■ For any non-null reference value x, x.equals(null) should return false.

List myList = new ArrayList();
As of Java 5 you'll want to say
List<String> myList = new ArrayList<String>();
This kind of declaration follows the object oriented programming principle of
"coding to an interface", and it makes use of generics.

List<String> test = new ArrayList<String>();
String s = "hi";
test.add("string");
test.add(s);
test.add(s+s);
System.out.println(test.size());
System.out.println(test.contains(42));
System.out.println(test.contains("hihi"));
test.remove("hi");
System.out.println(test.size());
which produces
3
false
true
2

There's lots going on in this small program. Notice that when we declared the
ArrayList we didn't give it a size. Then we were able to ask the ArrayList for
its size, we were able to ask it whether it contained specific objects, we removed an
object right out from the middle of it, and then we rechecked its size.

List myInts = new ArrayList(); // pre Java 5 declaration
myInts.add(new Integer(42)); // had to wrap an int
As of Java 5 we can say
myInts.add(42); // autoboxing handles it!

In this last example, we are still adding an Integer object to myInts (not an int
primitive); it's just that autoboxing handles the wrapping for us.

Of course, ArrayList doesn't give you any way to sort its contents,
but the java.util.Collections class does
import java.util.*;
class TestSort1 {
public static void main(String[] args) {
ArrayList<String> stuff = new ArrayList<String>(); // #1
stuff.add("Denver");
stuff.add("Boulder");
stuff.add("Vail");
stuff.add("Aspen");
stuff.add("Telluride");
System.out.println("unsorted " + stuff);
Collections.sort(stuff); // #2
System.out.println("sorted " + stuff);
}
}
This produces something like this:
unsorted [Denver, Boulder, Vail, Aspen, Telluride]
sorted [Aspen, Boulder, Denver, Telluride, Vail]

class DVDInfo implements Comparable<DVDInfo> { // #1
// existing code
public int compareTo(DVDInfo d) {
return title.compareTo(d.getTitle()); // #2
} }
In line 1 we declare that class DVDInfo implements Comparable in such a way
that DVDInfo objects can be compared to other DVDInfo objects. In line 2 we
implement compareTo() by comparing the two DVDInfo object's titles. Since we
know that the titles are Strings, and that String implements Comparable, this is an
easy way to sort our DVDInfo objects, by title. Before generics came along in Java 5,
you would have had to implement Comparable something like this:
class DVDInfo implements Comparable {
// existing code
public int compareTo(Object o) { // takes an Object rather
// than a specific type
DVDInfo d = (DVDInfo)o;
return title.compareTo(d.getTitle());
} }

It’s important to remember that when you override equals() you MUST
take an argument of type Object, but that when you override compareTo() you
should take an argument of the type you’re sorting.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答这个问题。 首先,我们需要了解恶意代码检测的三种方法: 1. 签名检测法:通过比对已知的恶意代码的特征码,来检测新的文件是否为恶意代码。 2. 行为检测法:通过监控文件系统、网络连接等,来检测代码的行为是否属于恶意行为。 3. 启发式检测法:通过分析代码的结构和执行流程,来判断代码是否为恶意代码。 接下来,我们可以使用Python来实现一个简单的恶意代码检测系统。代码如下: ```python import os # 签名检测法 def signature_detection(file_path): virus_signature = ['Trojan', 'Malware', 'Virus'] # 已知的恶意代码特征码 with open(file_path, 'rb') as f: content = f.read() for signature in virus_signature: if signature in content: print(f'{file_path} is a virus based on signature detection.') return True return False # 行为检测法 def behavior_detection(file_path): if os.path.splitext(file_path)[1] in ['.exe', '.dll', '.sys']: # 只检测可执行文件、动态链接库和系统文件 # 这里只是简单的判断文件类型,实际上还需要更严谨的筛选条件 print(f'{file_path} is being monitored for behavior detection.') # 这里可以使用第三方库,如pywin32来监控文件系统、网络连接等 # 如果检测到恶意行为,可以返回True return False # 启发式检测法 def heuristic_detection(file_path): with open(file_path, 'r') as f: content = f.read() if 'eval(' in content: # 检测到eval()函数,可能存在恶意代码 print(f'{file_path} is a virus based on heuristic detection.') return True return False # 扫描指定文件路径下的所有文件 def scan_files(folder_path): for root, dirs, files in os.walk(folder_path): for file_name in files: file_path = os.path.join(root, file_name) if signature_detection(file_path) or behavior_detection(file_path) or heuristic_detection(file_path): print(f'{file_path} is a virus.') if __name__ == '__main__': folder_path = r'C:\Users\86136\Desktop\源代码001\文件\样本' scan_files(folder_path) ``` 在上面的代码中,我们首先定义了三个检测函数,分别对应签名检测法、行为检测法、启发式检测法。然后,我们定义了一个扫描函数`scan_files`,该函数会扫描指定文件路径下的所有文件,并分别使用三种检测方法来检测每个文件。如果检测到恶意代码,就会输出文件路径。 最后,在`if __name__ == '__main__':`中,我们指定了要扫描的文件路径,并调用`scan_files`函数来执行扫描操作。 需要注意的是,这只是一个简单的示例,实际上恶意代码检测需要更加严谨的方法和更加复杂的算法。同时,我们也需要考虑如何处理误报和漏报的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值