While I am not sure why you want to do it like this, please find below the working code. Please note the comments in line. I hope this helps.
class test{
public function __construct() {
add_action( 'wp_ajax_test2', array( $this, 'test2' ) );
/* Front end ajax needs this. */
add_action( 'wp_ajax_nopriv_test2', array( $this, 'test2' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'test1' ) );
}
public function test1() {
/* in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value */
wp_localize_script('test-script-ajax', 'ajaxobj', array('ajaxurl' => admin_url('admin-ajax.php')));
/* Moved your js to a separate js file and enquing it the WordPress way */
wp_enqueue_script( 'test-script-ajax', get_stylesheet_directory_uri() . '/js/test-ajax.js', array( 'jquery' ));
}
public function test2(){
echo "success";
exit;
}
}
$ob_call = new test;
$ob_call->test1();
Here is the code in test-ajax.js file
jQuery(document).ready(function($){
var data = { 'action':'test2' };
jQuery.post( ajaxobj.ajaxurl, data, function( response ) {
alert(response);
});
});