Home Java javaTutorial How to get the attributes and type of a class in Java?

How to get the attributes and type of a class in Java?

Jul 03, 2017 am 09:27 AM
Attributes get

import Java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class test {
 public static void main(String[] args) throws Exception {
//  BufferedReader bb=new BufferedReader(new InputStreamReader(System.in));
//  String classname=bb.readLine();
  Class c=Class.forName("ArrayListTest.java");
  //-------------------获取方法的详细信息
  Method m[]=c.getDeclaredMethods();
  for(int i=0;i<m.length;i++)
  {
   //--------------------获得方法的名字
   System.out.println("方法的名字是:"+m[i].getName());
   //--------------------获得方法参数的类型和有几个参数
   Class b[]=m[i].getParameterTypes();//获得所有的参数并且存放到数组B中
   for(int j=0;j<b.length;j++)
   {
    System.out.println("参数的类型是"+b[j]);
   }
   //--------------------获得方法返回值的类型
   System.out.println(m[i].getReturnType());//获得方法的返回值类型
   //--------------------获得方法的修饰符
   int mod=m[i].getModifiers();
   System.out.println("方法的修饰符有"+Modifier.toString(mod));
   //--------------------获得方法的异常类型
   Class e[]=m[i].getExceptionTypes();//获得所有的异常类型存放到数组e中
   for(int k=0;k<e.length;k++)
   {
    System.out.println("方法的异常类型是:"+e[k]);
   }
   System.out.println("-------------------------------------------------------------------");
  }
  //----------------------------获得属性的详细信息
  
 }
}
package Class     //测试.com.tc.test.Class的forname方法获得属性信息;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class test {
 public static void main(String[] args) throws ClassNotFoundException {
  Class c=Class.forName("Class测试.com.tc.test.Class的forname方法获得属性信息.student");//把要使用的类加载到内存中,并且把有关这个类的所有信息都存放到对象c中
  Field f[]=c.getDeclaredFields();//把属性的信息提取出来,并且存放到field类的对象中,因为每个field的对象只能存放一个属性的信息所以要用数组去接收
  for(int i=0;i<f.length;i++)
  {
   System.out.println("属性的名称是:"+f[i].getName());//获得属性的名字
   System.out.println("属性的类型是:"+f[i].getType());//获得属性的类型
   int mod=f[i].getModifiers();//获得修饰符的常量总和
   System.out.println(mod);
   System.out.println("属性的修饰符有:"+Modifier.toString(mod));//modifier类可以根据常量总和去计算到底有哪些修饰符
   System.out.println("-------------------------------------------------------");
  }
 }
}
Copy after login


The above is the detailed content of How to get the attributes and type of a class in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the Crimson Abyss of War Double Pamish Lucia How to get the Crimson Abyss of War Double Pamish Lucia Mar 25, 2024 pm 05:31 PM

Players can obtain Lucia's Crimson Abyss when playing in Battle Double Pamish. Many players don't know how to obtain Lucia's Crimson Abyss. Players can obtain it through research and development, or redeem it at the Phantom Pain Cage store. How to obtain R&amp;D for Battle Double Pamish Lucia Crimson Abyss 1. Players can obtain it by drawing from the R&amp;D system, which includes the base card pool, the theme limited card pool and the destiny limited card pool. 2. Revealed in these card pools The basic drop rate of Sia Crimson Abyss is 1.50%, but if the player draws Lucia Crimson Abyss from the card pool, the drop rate will increase to 1.90%. Redemption in the Phantom Pain Cage Store 1. Players can redeem fragments of Lucia Crimson Abyss by using Phantom Pain Scars in the Phantom Pain Cage Store. 2. You can redeem up to 30 fragments every week.

How to obtain administrator rights in Win11 system How to obtain administrator rights in Win11 system Mar 08, 2024 pm 10:00 PM

It is very important to obtain administrator rights in the Win11 system, because administrator rights allow users to perform various operations in the system, such as installing software, modifying system settings, etc. Obtaining administrator rights in Win11 system can be achieved through the following methods: The first method is through user account control settings. In the Win11 system, User Account Control is a function used to manage user permissions. Through it, users can adjust their permission levels. To obtain administrator rights, users can enter the "Settings" interface and select "

How to get Phantom Beast Palu Wool How to get Phantom Beast Palu Wool Feb 03, 2024 pm 12:33 PM

There are various props and materials in the fantasy beast handkerchief, among which wool is an important resource. Wool can be used to make various props, but some players may not know how to obtain wool. Below I will introduce you to some methods of obtaining wool, I hope it will be helpful to you. How to obtain the wool of the fantasy beast Palu 1. The wool dropped by the palu can be obtained from a variety of palu, including Mian Youyou, Milufi, Melupa, Mao Shan'er, Mao Daddy, King Melupa and Ice Emperor. Melupa and seven other pallu. When fighting them, watch out for wool drops. 2. In addition to obtaining pasture production through combat, players can also easily produce wool in the pasture. The job adaptability in the game includes grazing pallu, such as Mianyouyou and Melupa. Wool can be harvested by grazing in the pasture.

Python's dir() function: View the properties and methods of an object Python's dir() function: View the properties and methods of an object Nov 18, 2023 pm 01:45 PM

Python's dir() function: View an object's properties and methods, specific code example required Summary: Python is a powerful and flexible programming language, and its built-in functions and tools provide developers with many convenient features. One of the very useful functions is the dir() function, which allows us to view the properties and methods of an object. This article will introduce the usage of the dir() function and demonstrate its functions and uses through specific code examples. Text: Python’s dir() function is a built-in function.

How to get Eldon's Ring Torret How to get Eldon's Ring Torret Mar 11, 2024 am 11:40 AM

Torret is the spirit horse in the game Elden's Circle. Many players don't know how to obtain Torret of Elden's Circle. To summon Torret, players need to obtain the spirit horse whistle, which is equipped in the shortcut bar. After that, use the shortcut keys to summon the spirit horse Torret. How to obtain Torret of Eldon's Ring? Answer: You need to obtain the Whistle of the Spirit Horse. 1. Players need to obtain the Spirit Horse Whistle to summon Torret. 2. Players go from the novice birth point to the blessing point in front of the Storm Road, sit down by the campfire, and the heroine [Melina] will appear, and she will give you a [Spirit Horse Whistle] ring. 3. After players equip the "Spirit Horse Whistle" to the shortcut bar and then use the Spirit Horse Whistle, they can summon the soul of Thoret's horse. 4. After riding the spirit horse Torret, you can perform a double jump. You can jump while walking but cannot jump.

How to get the white-haired pipe in Gulong Fengyunlu How to get the white-haired pipe in Gulong Fengyunlu Feb 29, 2024 pm 01:04 PM

There are many achievements in the Gulong Fengyunlu game, some of which are related to collectibles. When we meet Sun Xiaohong, it will also trigger a story with Sun Baifa, in which Baifa Pipe needs to get this pipe to complete it. The following is Everyone brings you the method to achieve the Gulong Fengyun Record White Hair Pipe. How to achieve Gu Long Fengyun Lu's White-haired Pipe 1. This achievement needs to be noted after the end of Chapter 4 of Sun Xiaohong's favor task. 2. Sun Xiaohong informed that her grandfather was missing, and then went to Luoyang Changting to find the whereabouts of Sun Baifa. 3. After learning that Sun Baifa had died at the hands of Shangguan Jinhong, he found Sun Baifa's tobacco stem on the ground and obtained the "White Hair Pipe". Key points: If Sun Xiaohong’s last mission is completed before returning to the main hall of Renyi Village in Chapter 4, Sun Baifa will not be able to trigger Luoyang City after entering the village.

bottom attribute syntax in CSS bottom attribute syntax in CSS Feb 21, 2024 pm 03:30 PM

Bottom attribute syntax and code examples in CSS In CSS, the bottom attribute is used to specify the distance between an element and the bottom of the container. It controls the position of an element relative to the bottom of its parent element. The syntax of the bottom attribute is as follows: element{bottom:value;} where element represents the element to which the style is to be applied, and value represents the bottom value to be set. value can be a specific length value, such as pixels

How to get the Wise Staff in Final Fantasy 7 Reborn How to get the Wise Staff in Final Fantasy 7 Reborn Mar 06, 2024 pm 10:37 PM

Many players don't know how to get the Wise Man's Staff in Final Fantasy 7 Rebirth. First, push the main story to Chapter 10 of the Magic Crystal Hunter's Determination. Go to the village service point of the Yi clan. You will see a treasure chest on the left. Open the treasure box and get the Wise Man's Staff. Staff, for details, let’s take a look at this article on how to obtain the Wise Man’s Staff in Final Fantasy 7 Reborn. Final Fantasy 7 Reborn Weapon Collection Guide How to get the Wise Man's Staff in Final Fantasy 7 Reborn Answer: Obtain it from the treasure chest on the left side of the service point in the Yi clan's village in Chapter 10. 1. Follow the main line to the determination of the magic crystal hunter in Chapter 10. 2. Arrive at the village of the Yi tribe. 3. Find a treasure chest to the left of the Yi tribe’s village service point. 4. Open the treasure chest to get the Wise Man’s Staff. After the Wise Man's Staff weapon skill accumulates ATB in the magic circle, it will give a certain amount of ATB to the partner.

See all articles