animal

Why don't I get InvalidCastException when casting enum to integer fails?

匿名 (未验证) 提交于 2019-12-03 08:41:19
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: public enum Animal { Dog = 1 , Cat = 2 , Cow = 3 } int animalID = 4 ; if (( Animal ) animalID == Animal . Dog ) // does not throw exception animalID can't be casted to Animal . Why don't I get InvalidCastException when casting enum to integer fails? 回答1: Because it's not an invalid cast. The value you are casting is out of range for the enum (in this case) but it's not invalid. As the approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong a cast from integer to enum is perfectly legal. Source - MSDN

静态类和非静态类的区别

让人想犯罪 __ 提交于 2019-12-03 08:06:22
静态和非静态的区别 静态: 使用static关键字 使用类名调用 在静态方法中,可以访问静态成员 在静态方法中,不可以直接访问实例成员 调用前需要初始化(构造方法) 非静态: 不需要使用static关键字 使用实例调用对象 在实例方法中可以直接访问静态成员 在实例方法中可以直接访问实例成员 实例化对象时需要初始化(构造方法) 对于静态类来说,在类中只能声明静态成员,相反实例类中可以声明静态类成员。 1 class Program{ 2 public static void Main(string[] args){ 3 Person.SayHello(); 4 Animal animal = new Animal(); 5 Console.ReadKey(); 6 } 7 } 8 static Person{ 9 static Person(){ 10 Console.WriteLine("我是静态类的构造方法"); 11 } 12 public static void SayHello(){ 13 Console.WriteLine("我是静态方法"); 14 } 15 } 16 class Animal{ 17 public Animal(){ 18 Console.WriteLine("我是实例化类的构造方法"); 19 } 20 } 在上面的demo中

内部类小结

雨燕双飞 提交于 2019-12-03 05:25:51
1、什么是内部类:就是在一个类的内部声明类。如: class Outer { class inner{ } } 关于如何访问内部类: class Outer { private int a = 1024; class Inner { public int b = 512; public void test1(){ System.out.println(a);//可以访问私有变量 } } public void test2(){ System.out.println(new Inner().b); } } public class Demo01 { public static void main(String[] args) { Outer.Inner inner = new Outer().new Inner(); inner.test1(); } } 从上面的例子可以看出内部类的声明方式: 外部类名.内部类名 对象名=new 外部类名().new 内部类名()。 另外:内部类可以访问外部类的成员变量,即使它是外部类所私有的。而外部类想要访问内部类的变量则必须创建该内部类对象才能访问。 2、上面的是一般的外部类,现在介绍 私有内部类 ,即用private修饰的内部类。 public class Demo01 { public static void main(String[]

What is this type of initialization called and why is it used? [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This question already has an answer here: Curly braces in “new” expression? (e.g. “new MyClass() { … }”) 2 answers Animal is a user defined class here. Animal D = new Animal("Leo") { @Override public void makeNoise() { System.out.println("Roar!"); } }; D.makeNoise(); 回答1: Its called an anonymous class and used to define the class and any overridden methods at the same time. 回答2: That is an anonymous class. For details about anonymous classes and why they are useful, see this tutorial on anonymous classes. 回答3: This is used to override the

Scala case class extending Product with Serializable

匿名 (未验证) 提交于 2019-12-03 02:45:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am learning scala and tried following form Scala Cookbook: trait Animal trait FurryAnimal extends Animal case class Dog(name:String) extends Animal case class Cat(name:String) extends Animal Now when I did following as : val x = Array(Dog("Fido"),Cat("Felix")) it show result as : x:Array[Product with Serializable with Animal] = Array(Dog(Fido),Cat(Felix)) Although I know that a case class is mixed in with Product trait What I am not getting is : Product with Serializable with Animal As per my understanding Product has something to do with

Using checkboxes and required with AngularJS

匿名 (未验证) 提交于 2019-12-03 02:45:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've tried to use the html5 required attribute for my group of checkboxes but I don't find a nice way to implement it with ng-form. When a checkbox is checked I want the value of that input-element to be pushed to an array of values. The angular required validator seems to watch the ng-model associated with the input element, but how can I link several checkboxes to the same model and update it's value with the value of the input field? Right now the implementation is like this fiddle . The updateQuestionValue-function handles the adding or

How to decide between an Interface or Base Class for an new implementation?

匿名 (未验证) 提交于 2019-12-03 02:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: When it comes to implementation, how should i decide to go for an base type or an Interface ? I tried to work out on few examples but i don't get the complete idea :( Examples on how and why would be greatly appreciated.. 回答1: A base class, abstract or not, can contain implemented members. An interface cannot. If all of your implementations are going to perform similarly, a base class might be the way to go because all of your child classes can share the same implementations of the members on the base class. If they aren't going to share

Sorting lines based on column

匿名 (未验证) 提交于 2019-12-03 02:41:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there any way where we can sort based on columns? I have lines like, 1000 Australia Kangaroo Canberra 1002 India Tiger Delhi 1092 Germany Eagle Berlin The above lines has to be sorted based on the second column, that is Australia, Germany, India. So, the result should be, 1000 Australia Kangaroo Canberra 1092 Germany Eagle Berlin 1002 India Tiger Delhi Data is from text file 回答1: I would suggest using a TreeSet and reading your text file and keeping your data in a class that implements Comparable . That way, when you're adding to the

Make @JsonTypeInfo property optional

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm using @JsonTypeInfo to instruct Jackson to look in the @class property for concrete type information. However, sometimes I don't want to have to specify @class , particularly when the subtype can be inferred given the context. What's the best way to do that? Here's an example of the JSON: { "owner": {"name":"Dave"}, "residents":[ {"@class":"jacksonquestion.Dog","breed":"Greyhound"}, {"@class":"jacksonquestion.Human","name":"Cheryl"}, {"@class":"jacksonquestion.Human","name":"Timothy"} ] } and I'm trying to deserialize them into these

Why use virtual functions? [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Possible Duplicate: Can someone explain C++ Virtual Methods? I have a question regarding to the C++ virtual functions. Why and when do we use virtual functions? Can anyone give me a real time implementation or use of virtual functions? 回答1: You use virtual functions when you want to override a certain behavior (read method) for your derived class rather than the one implemented for the base class and you want to do so at run-time through a pointer to the base class. The classic example is when you have a base class called Shape and concrete