如何将新实例的字段从一个类转到另一个类

时间:2018-03-29 18:01:13

标签: c# object

因此,我正在实施一个基本的酒店预订系统,其中包括入住和退房方式。对于签入和签出选择,我有一个switch语句,允许用户通过选择一个数字来选择一个房间。我有一个截断的下面代码版本,因为case语句的性质几乎相似:

case 1:
        if (true)//this is supposed to check if the room is booked
        {
             Console.WriteLine("This room is already booked, please try another place");
         }
        else
        {
           var instance1 = new BookingMethods();
           instance1.bookRoom1();
           revenueGenerated += 100; 
         }
        break;

对于if语句,我正在尝试检查房间是否被预订,并且我已经在另一个类中创建了用于签入和签出方法的对象。以下是我这样做的方式:

class BookingMethods
{
    Room room1 = new Room();

    public void bookRoom1()
    {
        //code for generating ints
        room1.occupied = true;
        room1.numGuests = guests1;
        room1.daysBooked = staying1;
        room1.roomType = "luxury";
    }
}

我希望if语句能够读取room1.occupied条件,但我似乎无法正确引用它。我在案例陈述bookRoom1()BookingMethods.cs Program.cs,我在Room。我想我不必将BookingMethods.cs对象从Program.cs移到import spotipy import requests import sys import spotipy.util as util from spotipy.oauth2 import SpotifyClientCredentials # Setup the credentials client_credentials_manager = SpotifyClientCredentials(client_id=client_id,client_secret=client_secret) # Make the connection sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager,auth=access_token) sp.current_user() --------------------------------------------------------------------------- HTTPError Traceback (most recent call last) ~\Anaconda\lib\site-packages\spotipy\client.py in _internal_call(self, method, url, payload, params) 118 try: --> 119 r.raise_for_status() 120 except: ~\Anaconda\lib\site-packages\requests\models.py in raise_for_status(self) 934 if http_error_msg: --> 935 raise HTTPError(http_error_msg, response=self) 936 HTTPError: 401 Client Error: Unauthorized for url: https://api.spotify.com/v1/me/ During handling of the above exception, another exception occurred: SpotifyException Traceback (most recent call last) <ipython-input-12-a36bf58a0704> in <module>() ----> 1 sp.current_user() ~\Anaconda\lib\site-packages\spotipy\client.py in current_user(self) 572 An alias for the 'me' method. 573 ''' --> 574 return self.me() 575 576 def current_user_saved_albums(self, limit=20, offset=0): ~\Anaconda\lib\site-packages\spotipy\client.py in me(self) 566 An alias for the 'current_user' method. 567 ''' --> 568 return self._get('me/') 569 570 def current_user(self): ~\Anaconda\lib\site-packages\spotipy\client.py in _get(self, url, args, payload, **kwargs) 144 while retries > 0: 145 try: --> 146 return self._internal_call('GET', url, payload, kwargs) 147 except SpotifyException as e: 148 retries -= 1 ~\Anaconda\lib\site-packages\spotipy\client.py in _internal_call(self, method, url, payload, params) 122 raise SpotifyException(r.status_code, 123 -1, '%s:\n %s' % (r.url, r.json()['error'] ['message']), --> 124 headers=r.headers) 125 else: 126 raise SpotifyException(r.status_code, SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/me/: Unauthorized. 来执行此操作,而我无法找到一种方法来调用它。

2 个答案:

答案 0 :(得分:1)

您没有为room1指定访问修饰符。如果您不提供一个,则默认设置为private,这意味着它只能在声明它的类中访问。除此之外,你应该真正创建一个属性。

这应该让你前进:

var instance1 = new BookingMethods();
switch (instance1.room1.occupied)
{
    case true:
         Console.WriteLine("This room is already booked, please try another place");
    break;
    default:
        instance1.bookRoom1();
        revenueGenerated += 100; 
    break;
}

class BookingMethods
{
    public Room room1 {get; set;};

    public void bookRoom1()
    {
        //code for generating ints
        room1.occupied = true;
        room1.numGuests = guests1;
        room1.daysBooked = staying1;
        room1.roomType = "luxury";
    }
}

答案 1 :(得分:0)

让room1成为一个领域

class BookingMethods
{
    Room room1 {get; set;};

    public void bookRoom1()
    {
        //code for generating ints
        room1.occupied = true;
        room1.numGuests = guests1;
        room1.daysBooked = staying1;
        room1.roomType = "luxury";
    }
}