java get set is_Is GET or SET defined in class

博客围绕在Java中判断类属性的getter和setter是否存在展开。作者在循环扩展类构造函数的选项时,想仅为当前类而非基类的属性赋值,尝试了多种方法,如getOwnPropertyDescriptor、hasOwnProperty等,但都存在问题,还探讨了避免问题的可选方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I try to find out, if the current derived class has a property setter. Reason is that I loop a couple of options through the constructors of extended classes. and just want to assign those properties, that are part of this class and not the base class.

So I need a possibility to find out, if the getter or setter exist for this class or not. I remove as much code as possible to show case the issue only.

This is the base class:

class baseClass{

constructor(wOptions){

//do some stuff

if(typeof wOptions == "object"){

for(let prop in wOptions){

// if(Object.getOwnPropertyDescriptor(this, prop) != undefined){

// if(this.hasOwnProperty(prop) != undefined){ /* doesn't work either */

if(typeof this[prop] != "undefined"){ /* calls the getter, if exists. */

this[prop] = wOptions[prop];

delete wOptions[prop];

}

}

}

}

get mainProperty(){

return 42;

}

set mainProperty(newValue){

return 42;

}

}

Here is the the derived class:

class derivatedClass extends baseClass{

constructor(wOptions){

super(wOptions)

//do some other stuff

let html = document.body;

Object.defineProperty(this, "html", {value: html, writable: false});

if(typeof wOptions == "object"){

for(let prop in wOptions){

// if(Object.getOwnPropertyDescriptor(this, prop) != undefined){

// if(this.hasOwnProperty(prop) != undefined){ /* doesn't work either */

if(typeof this[prop] != "undefined"){ /* calls the getter, if exists. */

this[prop] = wOptions[prop];

delete wOptions[prop];

}

}

}

}

get otherProperty(){

return html.innerText;

}

set otherProperty(newValue){

return html.innerText = newValue;

}

}

And how to initialize the object:

let options = {otherProperty: "new Text"};

let object = new derivatedClass(options);

console.log(options);

Regarding the solutions tried already:

getOwnPropertyDescriptor always returns undefined; returns the options as assigned

hasOwnProperty always returns false ; returns the options as assigned

typeof this[prop] != "undefined" calls the getter and this can be pretty bad, because html is not defined yet. Reference Error for html.innerText

Not a solution, but for verification: Removing the if clause in the derived class, changes the body text to new Text and prints an empty object in the console.

Tested in Chrome 71.

There would be some options to avoid this:

shift all properties handled by this class to another object (pretty ugly and high possibility to forget a property in the list)

use always Object.defineProperty, because they will be executed after the super call. However, it is not that pretty than the get/set functions of the class construct.

Is there any possibility to find out if there is a setter available for otherProperty, that evaluates true in the derivatedClass but not in the baseClass?

@echo off setlocal chcp 65001 > nul set CURRENT_DIR=%cd% cd .. set CAF_HOME=%cd% cd %CURRENT_DIR% if not exist %CAF_HOME%\bin\caf-server.bat ( goto end ) set CAF_BASE=%CAF_HOME% call %CAF_BASE%\bin\config.bat set HBN_TOOL=%CAF_BASE%\bin\hibernate-selector.bat if not exist %HBN_TOOL% ( goto end ) call %HBN_TOOL% %* set CLASSPATH= set CAF_BOOTSTRAP=%CAF_HOME%\%CAF_SERVER_PATH%\runtime\caf-bootstrap.jar set CAF_BOOTSTRAP_LANUCHER=org.springframework.boot.loader.PropertiesLauncher set CAF_MODULE_PATHS="%CAF_HOME%\%CAF_SERVER_PATH%\runtime\3rd","%CAF_HOME%\%CAF_SERVER_PATH%\runtime\libs" set CAF_BOOT_CONFIG_PATH=%CAF_HOME%/%CAF_SERVER_PATH%/runtime/ set JAVA_OPTS=%JAVA_OPTS% -Dparallel.startup=%CAF_PARALLEL_INIT% set /a totalMem=16777216+1 for /f "skip=1" %%i in ('wmic os get TotalVisibleMemorySize') do ( if %%i geq 0 set totalMem=%%i ) set /a "memorySize=%totalMem%/1024" set /a "totalMem=%memorySize%/10*6" set XmxSize=%totalMem%M if not defined CAF_MEM_OPTS ( if %memorySize% leq 16384 set CAF_MEM_OPTS=-Xmx%XmxSize% ) if not exist %CAF_HOME%\bin\java-home.bat ( goto end ) call %CAF_HOME%\bin\java-home.bat %1 if errorlevel 1 goto end if "%CLASSPATH%" == """" goto emptyClasspath set "CLASSPATH=%CLASSPATH%;" :emptyClasspath set CLASSPATH=%CLASSPATH%;%CAF_HOME%\%CAF_SERVER_PATH%\runtime\caf-bootstrap.jar if "%CAF_TMPDIR%" == """" goto gotTmpdir :gotTmpdir set CAF_TMPDIR=%CAF_BASE%\temp if not "%JSSE_OPTS%" == """" goto gotJsseOpts set JSSE_OPTS=-Djdk.tls.ephemeralDHKeySize=2048 :gotJsseOpts set JAVA_OPTS=%JAVA_OPTS% %JSSE_OPTS% set JAVA_OPTS=%JAVA_OPTS% -Djava.protocol.handler.pkgs=org.apache.caf.webresources set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.base/java.lang=ALL-UNNAMED" set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.base/java.io=ALL-UNNAMED" set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.base/java.util=ALL-UNNAMED" set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.base/java.util.concurrent=ALL-UNNAMED" set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED" set ENDORSED_PROP=ignore.endorsed.dirs if "%JAVA_ENDORSED_DIRS%" == """" goto noEndorsedVar set ENDORSED_PROP=java.endorsed.dirs goto doneEndorsed :noEndorsedVar if not exist %CAF_HOME%\endorsed goto doneEndorsed set ENDORSED_PROP=java.endorsed.dirs :doneEndorsed set MAINCLASS=%CAF_BOOTSTRAP_LANUCHER% --spring.config.location=%CAF_BOOT_CONFIG_PATH% set ACTION=start set SECURITY_POLICY_FILE= set DEBUG_OPTS= set JPDA= set DEBUG_PORT="5005" if ""%1"" == ""run"" goto doRun if ""%1"" == ""start"" goto doStart goto end :doRun shift if not ""%1"" == ""-security"" goto execRunCmd shift set "SECURITY_POLICY_FILE=%CAF_BASE%\conf\caf.policy" goto execRunCmd :execRunCmd set CMD_LINE_ARGS= :setRunArgs if ""%1""=="""" goto doneRunSetArgs set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 shift goto setRunArgs :doneRunSetArgs "%JAVA_HOME%\bin\java" %CAF_OPTS% -server -Djava.io.tmpdir=%CAF_TMPDIR% -Dloader.path=%CAF_MODULE_PATHS% -Dfile.encoding=UTF-8 %JAVA_OPTS% -Dserver.runtime.path.name=%CAF_SERVER_PATH% %CAF_MEM_OPTS% -Dspring.profiles.active=prod -classpath %CLASSPATH% %MAINCLASS% goto end :doStart shift if "%TITLE%" == """" set TITLE=iGIX Server set _EXECJAVA=start "%TITLE%" cmd /k if not ""%1"" == ""-security"" goto execStartCmd shift set "SECURITY_POLICY_FILE=%CAF_BASE%\conf\caf.policy" goto execStartCmd :execStartCmd set CMD_LINE_ARGS= :setStartArgs if ""%1""=="""" goto doneSetStartArgs set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 shift goto setStartArgs :doneSetStartArgs %_EXECJAVA% "chcp 65001 > nul && %_RUNJAVA% %CAF_OPTS% -server -Djava.io.tmpdir=%CAF_TMPDIR% -Dloader.path=%CAF_MODULE_PATHS% -Dfile.encoding=UTF-8 %JAVA_OPTS% -Dserver.runtime.path.name=%CAF_SERVER_PATH% %CAF_MEM_OPTS% -Dspring.profiles.active=prod -classpath %CLASSPATH% %MAINCLASS% %CMD_LINE_ARGS%" goto end :end详细解释一下
最新发布
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值