PauLinhu221 Posted April 29, 2021 at 05:21 PM Report Share #622144 Posted April 29, 2021 at 05:21 PM (edited) Boas, estou a tentar fazer uma lista para um inventário simples, mas deparei-me com o problema de que a Lista tem que levar um tipo. Sendo que tenho uma classe para cada item, como faria? Thanks in advance. (O código abaixo é um exemplo abstrato para o que pretendo, n foi compilado) //Inventory.cs public class Inventory { public List<Item> itemList; public Inventory() { itemList = new List<Item>(); } } //Keyboard.cs public class Keyboard { public int keys; public Keyboard() { keys = 67; } } //Monitor.cs public class Monitor { public int refreshRate; public Monitor() { refreshRate=60; } } Edited April 29, 2021 at 05:22 PM by PauLinhu221 Link to comment Share on other sites More sharing options...
bruno1234 Posted April 29, 2021 at 05:41 PM Report Share #622145 Posted April 29, 2021 at 05:41 PM Coloca as classes a extenderem de Item Matraquilhos para Android. Gratuito na Play Store. https://play.google.com/store/apps/details?id=pt.bca.matraquilhos Link to comment Share on other sites More sharing options...
PauLinhu221 Posted April 29, 2021 at 06:03 PM Author Report Share #622149 Posted April 29, 2021 at 06:03 PM (edited) 22 minutos atrás, bruno1234 disse: Coloca as classes a extenderem de Item public class Item { public class Keyboard : Item { public int keys; public Keyboard() { keys=67; } } public class Monitor : Item { public int refreshRate; public Monitor() { refreshRate=60; } } } Assim? Edited April 29, 2021 at 06:03 PM by PauLinhu221 Link to comment Share on other sites More sharing options...
antseq Posted April 30, 2021 at 08:11 AM Report Share #622159 Posted April 30, 2021 at 08:11 AM 14 horas atrás, PauLinhu221 disse: Assim? Penso que seria melhor assim e em ficheiro separados: public class Item { ... } public class Keyboard : Item { ... } public class Monitor : Item { ... } Se tiveres propriedades comuns (id, serial, brand, category, color, size, ...) a todos os "Items" deves colocar em "Item". cps, Link to comment Share on other sites More sharing options...
PauLinhu221 Posted April 30, 2021 at 09:40 AM Author Report Share #622162 Posted April 30, 2021 at 09:40 AM 1 hora atrás, antseq disse: Penso que seria melhor assim e em ficheiro separados: public class Item { ... } public class Keyboard : Item { ... } public class Monitor : Item { ... } Se tiveres propriedades comuns (id, serial, brand, category, color, size, ...) a todos os "Items" deves colocar em "Item". cps, Certo, e com a lista criada como acederia por exemplo á propriedade keys de um objeto do tipo Keyboard instanciado? Thx Link to comment Share on other sites More sharing options...
M6 Posted May 3, 2021 at 08:42 AM Report Share #622189 Posted May 3, 2021 at 08:42 AM Podes criar uma interface com uma assinatura que todas as tuas classes são obrigadas a respeitar. Por exemplo, todas implementaria a propriedade "keys". 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar." Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now