现在我有一个 BaseObject 可以对 DB 执行 ORM。我依靠私有 $data 和魔法 setter 和 getter 来创建带有一堆列的对象作为私有对象成员(动态)。在子类中,如果我想更改行为以设置单个对象成员,我必须覆盖父 setter 并查找键。我的问题是是否有更好的方法来做到这一点,我可以只覆盖单个对象成员而不是通过 __setter
映射到数据库并动态创建一堆私有参数的基本对象映射器
class Base
{
private $data = array();
public function __construct(){
//get columns info from db and do set
}
public function __set($key, $value){
$this->data[$key] = $value;
}
public function __get($key){
return isset($this->data[$key])? $this->data[$key] : null;
}
}
Run Code Online (Sandbox Code Playgroud)
和儿童班。现在要覆盖参数设置我必须这样做
class Child extends Base
{
public function __set($name, $value){
if($name == 'dog' && $value == 'chihuahua')
$this->dog = 'dog bark wolf wolf';
else if($name == 'cat' …Run Code Online (Sandbox Code Playgroud) 我试图理解我在这里做错了什么,但它对我来说真的没有意义.我有一个类calleld LatLongBean在这里我试图解析XML提要.
我有一个方法,我做所有的逻辑.我有一些吸气剂和二传手.
看起来安装员工作正常,但吸气剂不起作用.
这是LatLongBean:
public class LatLongBean {
private String lat;
private String lng;
private String address;
private String url = "http://maps.googleapis.com/maps/api/geocode/xml?address=";
public void LatLongBean(String address, String lat, String lng) throws ParserConfigurationException, SAXException, IOException {
this.address = address;
this.lat = lat;
this.lng = lng;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new URL(url + address + "&sensor=false").openStream());
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("location");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = …Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,我从服务器获取数据,我使用getter和setter函数来设置这些.
这是我的代码......
JSONArray arr1 = new JSONArray(strServerResponse);
JSONObject jsonObj1 = arr.getJSONObject(0);
pojo = new Pojo();
empid = jsonObj1.optString("empid");
pojo.setId(empid);
Run Code Online (Sandbox Code Playgroud)
而我正在使用getter函数
Pojo pojo = new Pojo();
String id = pojo.getId();
Run Code Online (Sandbox Code Playgroud)
这是我的setter和getter函数
public class Pojo {
private String empid;
public void setId(String empid) {
this.empid = empid;
}
public String getId() {
return empid;
}
}
Run Code Online (Sandbox Code Playgroud)
我在使用getter函数的地方得到Null Pointer Exception.我做错了吗?谁能帮帮我吗.
相信并使用我应该使用哪个 @NotNull Java 注释?,我有一个类,其中某些字段标记为@NotNull[ package javax.validation.constraints] 以传递给客户端。该类还为这些字段实现了默认的 getter 和 setter。下面的示例类 -
public class MyClass
{
public MyClass() {
}
@NotNull
private String name;
private Boolean bool;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean isBool() {
return bool;
}
public void setBool(Boolean bool) {
this.bool = bool;
}
}
Run Code Online (Sandbox Code Playgroud)
我对业务逻辑中 getter 的用法感到有些困惑 -
if(new MyClass().getName() !=null) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
这个
null检查是不是多余的,(如果不是)想知道 …
有什么方法可以只实现 setter 并get;在 c# 或其他方式中继续使用速记 ( ) 作为 getter 吗?
例如:在设置时将电子邮件转换为小写 -
public string Email { get; set { Email = value.ToLower(); } }
Run Code Online (Sandbox Code Playgroud)
我们可以以任何方式做到这一点吗?
我一直在使用 Kotlin 在 Android 中开发一些应用程序,我目前想要做的是在定义类中设置一个字段值而不调用 setter 方法。
这是我班级中的代码:
var projectList: List<Project>? = null
set(value) {
saveProjects(value as ArrayList<Project>)
field = value
}
//GO to the database and retrieve list of projects
fun loadProjects(callback: Project.OnProjectsLoadedListener) {
database.projectDao().getAll().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ success ->
callback.onProjectsLoaded(success)
//Here i don't want to save the projects, because i've loaded them from the database
this.projectList = success
},
{ error -> GenericErrorHandler.handleError(error,callback.retrieveContext())}
)
}
Run Code Online (Sandbox Code Playgroud)
有人知道不调用方法就可以做到这一点的set(value)方法吗?
我这里有一条简单的逻辑链,其中bigCities是一个Javascript Map。在此示例中,d表示从csv文件读取的数据数组中的每个对象。对于每个对象的城市属性,我将其分配为d.pop值(城市人口)。
bigCities.set(d.city, +d.pop)
如果我希望能够一次设置多个值怎么办?代码看起来像这样:
bigCities.set(d.city, ["population": +d.pop, "latitude": +d.lat, "longtitude": +d.lng)
Run Code Online (Sandbox Code Playgroud)
是否可以在Javascript映射中创建键值对,其中值是数据数组?如果是这样,我将如何正确编写上面的示例?
我正在使用 proguard GUI 来混淆我的 java 代码。
我的要求是我不想混淆我的 Getter 和 setter。proguard GUI 中是否有任何选项可以单独排除 Getter、setter 的混淆。
如果是这样,请为我提供示例指定的位置。
我正在努力寻找在类中初始化(私有)数组并从类外部获取/设置值的正确格式。
我的代码是半功能的,但在格式不正确时感觉很尴尬。 它只返回数组的第一个元素,我希望它返回所有内容。阅读代码注释以获取更多详细信息。
注意:这是我正在为学校工作的项目(其中很小的一部分)——必须使用数组,而不是向量或列表。
学生.h
class Student {
public:
// Upon researching my issue, I read suggestions on passing pointers for arrays:
void SetDaysToCompleteCourse(int* daysToCompleteCourse[3]);
int* GetDaysToCompleteCourse(); // Ditto @ above comment.
private:
int daysToCompleteCourse[3];
Run Code Online (Sandbox Code Playgroud)
学生.cpp
#include "student.h"
void Student::SetDaysToCompleteCourse(int* daysToCompleteCourse) {
// this->daysToCompleteCourse = daysToCompleteCourse; returns error (expression must be a modifiable lvalue)
// Feels wrong, probably is wrong:
this->daysToCompleteCourse[0] = daysToCompleteCourse[0];
this->daysToCompleteCourse[1] = daysToCompleteCourse[1];
this->daysToCompleteCourse[2] = daysToCompleteCourse[2];
}
int* Student::GetDaysToCompleteCourse() {
return daysToCompleteCourse;
}
Run Code Online (Sandbox Code Playgroud)
控制台应用程序1.cpp
#include "pch.h"
#include …Run Code Online (Sandbox Code Playgroud) 我正在努力弄清楚this上课的目的。考虑以下示例:
class TestMe {
var a: Int = 1
var b: Int = 2
fun setValA(value: Int) {
this.a = value
}
fun setValB(value: Int) {
b = value
}
}
val testInstance1 = TestMe()
val testInstance2 = TestMe()
testInstance1.setValA(3)
testInstance1.setValB(4)
println("instance 2A: ${testInstance2.a}, instance 2B: ${testInstance2.b}") // 1 2
println("instance 1A: ${testInstance1.a}, instance 1B: ${testInstance1.b}") // 3 4
Run Code Online (Sandbox Code Playgroud)
似乎我可以简单地省略thisvalue 并且结果将是相同的。有什么我在这里想念的吗?
非常感谢!
getter-setter ×10
java ×3
android ×2
arrays ×2
kotlin ×2
c# ×1
c++ ×1
class ×1
ecmascript-6 ×1
javascript ×1
mapper ×1
notnull ×1
null ×1
obfuscation ×1
php ×1
proguard ×1
this ×1