animal

Vector of objects belonging to a trait

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Consider the following code: trait Animal { fn make_sound(&self) -> String; } struct Cat; impl Animal for Cat { fn make_sound(&self) -> String { "meow".to_string() } } struct Dog; impl Animal for Dog { fn make_sound(&self) -> String { "woof".to_string() } } fn main () { let dog: Dog = Dog; let cat: Cat = Cat; let v: Vec<Animal> = Vec::new(); v.push(cat); v.push(dog); for animal in v.iter() { println!("{}", animal.make_sound()); } } The compiler tells me that v is a vector of Animal when I try to push cat (type mismatch) So, how can I make a

Shorter syntax for casting from a List&lt;X&gt; to a List&lt;Y&gt;?

匿名 (未验证) 提交于 2019-12-03 01:58:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I know its possible to cast a list of items from one type to another (given that your object has a public static explicit operator method to do the casting) one at a time as follows: List ListOfY = new List (); foreach(X x in ListOfX) ListOfY.Add((Y)x); But is it not possible to cast the entire list at one time? For example, ListOfY = (List )ListOfX; 回答1: If X can really be cast to Y you should be able to use List listOfY = listOfX.Cast ().ToList(); Some things to be aware of (H/T to commenters!) You must include using System.Linq; to get

Is it possible to have class without variables and with methods?

匿名 (未验证) 提交于 2019-12-03 01:52:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm solving this problem with polymorphism. I need to print out verse of the song with 5 animals.The verse is repeated for each animal and the appropriate sound for the animal is used eg cows go “moo”, ducks go “quack” etc. I would like to ask is it possible to have a class only with methods. In addition here is my code. public class Animal { public virtual void PrintSong () { } } public class Cow : Animal { public override void PrintSong () { Console . WriteLine ( "I go \"Mooo\" (I'm a cow, I'm a cow, I'm a cow)" ); } } public

Error in boost-python nested namespace exporting code

匿名 (未验证) 提交于 2019-12-03 01:41:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I am working on exporting two classes class zoo (extzoo and intzoo namespaces), class animal (extanim and intanim namespaces)" methods which are defined within two level nested namespaces. I want to expose these methods to Python interpreter from where I can access them. I have written code, created a shared library but when I import it into a python I get an error. I will appreciate your guidance on this. I have followed the answer given on the following link in similar context of exposing C++ nested namespaces: create boost

Subtotals for Pandas pivot table index and column

匿名 (未验证) 提交于 2019-12-03 01:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'd like to add subtotal rows for index #1 (ie. Fruits and Animal) and subtotal columns for columns (ie. 2015 and 2016). For the subtotal columns, I could do something like this, but it seems inefficient to run this type of code for each year (2015 & 2016). Is there a better way? I don't think 'margins' will work because there are multiple subtotals. df[('2015','2015_Total')] = df[('2015','1st')]+df[('2015','2nd')] For the subtotal rows (e.g., fruits total and animal total), I'm not sure where to begin. 回答1: It is very complicated, because

C# Casting MemoryStream to FileStream

匿名 (未验证) 提交于 2019-12-03 01:33:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: My code is this: byte[] byteArray = Encoding.ASCII.GetBytes(someText); MemoryStream stream = new MemoryStream(byteArray); StreamReader reader = new StreamReader(stream); FileStream file = (FileStream)reader.BaseStream; Later I'm using file.Name. I'm getting an InvalidCastException: it displays follows Unable to cast object of type 'System.IO.MemoryStream' to type 'System.IO.FileStream'. I read somewhere that I should just change FileStream to Stream. Is there something else I should do? 回答1: A MemoryStream is not associated with a file, and

How to implement the Java comparable interface?

匿名 (未验证) 提交于 2019-12-03 01:15:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am not sure how to implement a comparable interface into my abstract class. I have the following example code that I am using to try and get my head around it: public class Animal{ public String name; public int yearDiscovered; public String population; public Animal(String name, int yearDiscovered, String population){ this.name = name; this.yearDiscovered = yearDiscovered; this.population = population; } public String toString(){ String s = "Animal name: "+ name+"\nYear Discovered: "+yearDiscovered+"\nPopulation: "+population; return s; }

Downcast from Any to a protocol

匿名 (未验证) 提交于 2019-12-03 01:14:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have the following code. How can I resolve the error in the last line? protocol Animal { func walk() } struct Cat: Animal { func walk() {} init() { } } var obj: Any = Cat() var cat = obj as Animal // ERROR: cannot downcast from Any to unrelated type Animal 回答1: Update: This has been fixed in Swift 1.2+ (Xcode 6.3+). The Xcode 6.3 beta release notes say: Dynamic casts (“as!", “as?" and “is”) now work with Swift protocol types, so long as they have no associated types. You can only check for protocol conformance (which includes is , as , and

Skip before_filter in Rails

匿名 (未验证) 提交于 2019-12-03 01:14:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Names and objects have been simplified for clarity's sake. The basic concept remains the same. I have three controllers: dog , cat , and horse . These controllers all inherit from the controller animal . In the controller animal , I have a before filter that authenticates a user as such: before_filter : authenticate def authenticate authenticate_or_request_with_http_basic do | name , password | name == "foo" && password == "bar" end end In the show action of dog , I need to have open access to all users (skip the authentication).

How do we define @list_route that accept arguments

匿名 (未验证) 提交于 2019-12-03 01:05:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In my application i have this ModelViewSet with one @list_route() defined function for getting list but with different serializer. class AnimalViewSet(viewsets.ModelViewSet): """ This viewset automatically provides `list`, `create`, `retrieve`, `update` and `destroy` actions. """ queryset = Animal.objects.all() serializer_class = AnimalSerializer // Default modelviewset serializer lookup_field = 'this_id' @list_route() def listview(self, request): query_set = Animal.objects.all() serializer = AnimalListingSerializer(query_set, many=True) //