开发者

Android: Class problem

开发者 https://www.devze.com 2023-02-27 07:30 出处:网络
I\'ve created a class called website, and want to acce开发者_StackOverflow中文版ss it like a variable so I can update values to it, probably better explained below:

I've created a class called website, and want to acce开发者_StackOverflow中文版ss it like a variable so I can update values to it, probably better explained below:

Website w = new Website();
w.URL="stackoverflow.com";

Here's the code for the class:

class Website {
         public String URL;
         public Website(){
             URL = "";
         }

}

I would also like to add a method such as this:

public long save() {
     return db.save(URL);
}

This (the method) isn't working for me at the moment


I would do it more OO way, hiding this URL variable from outside and letting change it's value from getter and setter methods. You can try this, maybe this will help.

In Website class

public class Website {
   private String URL;

   public Website(){
      this.URL = "";
   }

   public void setUrl(String url) {
      this.URL = url;
   }

   public String getUrl() {
      return this.URL;
   }

  public long save() {
     return db.save(this.URL);
  }

}

And then call it

Website w = new Website();
w.setUrl("http://www.stackoverflow.com");
long someLongValue = w.save();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号