c# - How to perform method -
i have class person
, 2 child classes staff
, student
, interface iperson
. have class database
, class gateway
. class database
has
private string name = "username";
and method
public void getname() {return id;}
both staff , student have getname() methods. need create request getname() student , staff classes database through gateway. class gateway has check if method getname() requested staff
(then return id
) or student
(then return "go away!"
). can please me that. thinking using gateway
interface of database
class, because trying learn c#, don't know how that. or maybe there's better way of doing this... please thanks
here's code:
public class staff : person { public staff() {} public staff(string id): base(id) {} public override string getname() { throw new notimplementedexception(); } public override void update(object o) { console.writeline(id + " notified {1}", id, o.tostring()); } public override void updatemessage(object p) { console.writeline(id + " notified new message in chat: {1}", id, p.tostring()); } }
public class student : person { public student() {} public student(string id): base(id) {} public override string getname() { throw new notimplementedexception(); } public override void update(object o) { console.writeline(id +" notified {1}", id, o.tostring()); } public override void updatemessage(object p) { console.writeline("message " + id + " {1}", id, p.tostring()); } }
public abstract class person : iperson { public string id; public person() { } public abstract string getname(); public person(string i) { this.id = i; } public abstract void update(object o); public abstract void updatemessage(object p); } public interface iperson { void update(object o); void updatemessage(object p); string getname(); }
class database { public string username = "username"; private string name = "user details"; private string grade = "user grade"; public string getname(object o) { if (o staff) { return name; } else { return "go away!"; } } public string getgrade() { return grade; } } public class gateway { public void dosomethingwithperson(iperson person) { string id = person.getname(); if (person student) { return "go away!"; } else if (person staff) { return name; } } }
this convoluted question. so, first off, i'd point out few style issues c#.
- your
database
class lowercase while rest cased consistently. methods inconsistent (for example, use idiomatic pascalcase methods, , camelcase or lowercase others). iperson
has no purpose here because can pass instances ofstaff
,student
aroundperson
, use in same way now. in cases, you'll want choose either interface or abstract base class, not both.- c# has notion of "properties", convenient syntax getters , setters. preferred public fields (as in
public string username
indatabase
class orpublic string id
inperson
) because allows keep implementation of backing field private. syntaxpublic string username { get; set; }
if want default implementation. can expand more complicated things. example, maybe want ensure username trimmed. (1) - minor nitpick, typically
object
lowercase o used. - you don't have call
.tostring()
on objects in string formatting interpolation. (2)
(1)
private string m_username; public string username { { return m_username; } set { m_username = (value != null ? value.trim() : value); } }
(2) these lines equivalent.
console.writeline(id + " notified {1}", id, o.tostring()); console.writeline("{0} notified {1}", id, o);
now on problem. me, sounds want different behavior different classes. way it's phrased, sounds access/permissions issue. depending on how data store set (in case, looks constants in code, kind of query), like...
[flags] public enum permission { none = 0, getname = 1 } public abstract class person { /* ... */ public abstract permission permissions { get; } } public class staff : person { /* ... */ public override permission permissions { { return permission.getname; } } } public class student : person { /* ... */ public override permission permissions { { return permission.none; } } } public class database { /* ... */ private dictionary<string, string> namesdatabase { get; set; } public string getname(string id) { // consequence of being managed gateway, assume caller has access return namesdatabase[id]; } } public class gateway { public string dosomethingwithperson(person person, string desirednamepersonid) { if (person.permissions.hasflag(permission.getname)) { database db = new database(); return db.getname(desirednamepersonid); } return "go away!"; } }
supposing have constructor database
such:
public database() { namesdatabase = new dictionary<string, string>(2); namesdatabase["id1"] = "student amy"; namesdatabase["id2"] = "staff mary"; }
and main
such:
static void main() { gateway gate = new gateway(); console.writeline("student id1 looks staff id2: {0}", gate.dosomethingwithperson(new student("id1"), "id2")); console.writeline("staff id2 looks student id1: {0}", gate.dosomethingwithperson(new staff("id2"), "id1")); console.readline(); }
the output is:
student id1 looks staff id2: go away! staff id2 looks student id1: student amy
feel free ask clarifying questions if part unclear or i'm way off mark in assessment.
Comments
Post a Comment