已经反复说过,除了equals()方法之外,不应该使用instanceof运算符,否则它是一个糟糕的OOP设计.
有人写道,这是一个繁重的操作,但似乎,至少java,处理得相当好(甚至比Object.toString()比较更有效).
有人可以解释,或指导我一些文章,解释为什么这是一个糟糕的设计?
考虑一下:
Class Man{
doThingsWithAnimals(List<Animal> animals){
for(Animal animal : animals){
if(animal instanceOf Fish){
eatIt(animal);
}
else if(animal instanceof Dog){
playWithIt(animal);
}
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
决定如何处理动物,取决于男人.男人的欲望也可能偶尔改变,决定吃狗,玩鱼,而动物不会改变.
如果您认为instanceof运算符不是正确的OOP设计,请告诉您如何在没有instanceof的情况下执行此操作,为什么?
谢谢.
以下陈述:
URLClassLoader ucl = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class<URLClassLoader> uclc = ucl.getClass();
Run Code Online (Sandbox Code Playgroud)
失败并出错:
Type mismatch: cannot convert from Class<capture#2-of ? extends URLClassLoader> to Class<URLClassLoader>
Run Code Online (Sandbox Code Playgroud)
我为什么需要演员?
我发现了几个帖子解释了为什么你不能反过来(将T分配给a),但那种(有点)是显而易见的.
注意:我在eclipse Luna下编码,所以我不知道它是否是Luna Quirk,或者是否有一些我在仿制药中真的不理解的东西.
我正在学习C++.我的教授使用了一些类似的代码
using filePath = std::string;
using setOfPaths = std::set<filePath>;
using iterOfSet = setOfPaths::iterator;
using listOfIter = std::list<iterOfSet>;
using iterList = listOfIter::iterator;
using fileName = std::string;
using mapOfFileName = std::map<fileName, listOfIter>;
using iterOfMap = mapOfFileName::iterator;
setOfPaths _setOfPaths;
mapOfFileName _mapOfFileName;
iterOfSet setIter;
Run Code Online (Sandbox Code Playgroud)
我想知道为什么我们使用using关键字.为什么我们不能简单地写
std::string filepath;
std::set<filepath> setOfPaths;
...
...
Run Code Online (Sandbox Code Playgroud)
拥有using关键字有什么好处?
我的同事和我正在讨论该File.delete()方法如何在Java中工作.
在我们的代码中:
File outFile = new File("/dir/name.ext");
if(outFile.exists())
outFile.delete();
FileInputStream inStream = new FileInputStream(outFile);
WriteFile.writeFile(inStream); // Writes the actual file
Run Code Online (Sandbox Code Playgroud)
writeFile出于安全原因,我不能包含此处的整个方法体,但在创建所需的数据库对象后,它将执行以下操作:
BufferedOutputStream out = null;
Object[] args = {"an_encrypted_data_clob_name_in_the_database"};
Class[] argTypes = {Class.forName("java.lang.String")};
Object result = WSCallHelper.jdbcCall(null, rs, "getCLOB", args, argTypes);
CLOB clob = (CLOB)result;
out = new BufferedOutputStream(clob.getAsciiOutputStream());
byte[] buffer = new byte[512];
int bytesRead = -1;
while((bytesRead = inStream.read(buffer)) > -1)
out.write(buffer, 0, bytesRead);
Run Code Online (Sandbox Code Playgroud)
我知道这是一个有点不清楚,但它的一般要点是,它创建AsciiOutputStream的Clob(是的,它应该是一个Clob),并将其写入inStream是从以前的方法传递的对象.
他们确信这不会因为File.delete(); …
我正在用cardLayout改变"视图"(这个类有一个JFrame变量).当用户点击新游戏按钮时会发生这种情况:
public class Views extends JFrame implements ActionListener {
private JFrame frame;
private CardLayout cl;
private JPanel cards;
private Game game;
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("New game")) {
cl.show(cards, "Game");
game.init();
this.revalidate();
this.repaint();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
game.loop();
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
游戏的循环方法和课程标题:
public class Game extends JPanel implements KeyListener {
public void loop() {
while (player.isAlive()) {
try {
this.update();
this.repaint();
// first class JFrame variable …Run Code Online (Sandbox Code Playgroud) 不确定这是否可能; 我承认我并不像我希望的那样擅长仿制药.
基本上,我想创建一个类 - >函数的映射,其中用于键的类是函数输入的类,就像这样(不合法的语法):
public static Map<Class<T>,Function<T,Expression>> STUFF = new HashMap<>();
{
STUFF.put(List.class, ListExpression::new);
STUFF.put(String.class, StringExpression::new);// this constructor must take string
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做:
Function<String,Expression> e = STUFF.get(o.getClass());
Expression ex = e.apply(o);
Run Code Online (Sandbox Code Playgroud)
它适合我的类型.
出于某种原因,我不明白为什么这个代码会打印出来,true并且false有关数组的特殊之处是它不包含这个注释吗?
如果您使用它,它会按预期工作getParameters.
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE_USE, ElementType.PARAMETER})
@interface Lel {
}
class Test {
public static void a(@Lel String args) {}
public static void b(@Lel String[] args) {}
public static void main(String[] args) throws Exception {
System.out.println(Test.class.getDeclaredMethod("a", String.class)
.getAnnotatedParameterTypes()[0].isAnnotationPresent(Lel.class));
System.out.println(Test.class.getDeclaredMethod("b", String[].class)
.getAnnotatedParameterTypes()[0].isAnnotationPresent(Lel.class));
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将AVLTree实现转换为堆样式数组,并且在泛型方面存在一些问题:
public class MyAVLTree<K extends Comparable<? super K>, E> implements
OrderedDictionary<K, E> {
class AVLNode implements Locator<K, E>{
// ...
}
// ....
public Locator<K,E> [] toBSTArray() {
AVLNode[] bArray = new AVLNode[size];
makeArray(root, 0, bArray); // recursion
return bArray;
}
}
Run Code Online (Sandbox Code Playgroud)
在该行AVLNode[] bArray = new AVLNode[size];我得到以下错误:
"无法创建MyAVLTree.AVLNode的通用数组"
我不明白我做错了什么.有帮助吗?
如何在流畅验证中按实体类型注入或查找验证器?我有以下课程并希望通过流畅的验证来验证实体
public class BaseEntity {}
public class Article :BaseEntity
{
public string Name {get;set;}
}
public class ArticleValidator : AbstractValidator<Article>
{
public ArticleValidator()
{
RuleFor(x => x.Name ).NotEmpty().Length(0,512);
}
}
Run Code Online (Sandbox Code Playgroud)
并有 BaseEntity 的扩展:
public static ValidationResult Validate(this BaseEntity entity)
{
//????and here how i can find entity validator by type of entity and validate it and return result.
}
public class ArticleService
{
public void AddArticle(Article aricle)
{
var result = article.Validate();
if(result.IsValid)
;.......
}
}
Run Code Online (Sandbox Code Playgroud) 我是java新手,我看到一些复杂的代码,同时在网上看到我的程序的解决方案
int a=5;
int b=10;
first:{
second:{
third:{
if(a==b>>1){
break second;
}
System.out.println(a);
}
System.out.println(b);
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释一下这类代码的意义是什么以及我们如何在程序中深入使用它而我无法在我的java书中找到这种类型的代码请提前帮助我